Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ignore routes in MVC6

I'm developing a very simple SPA style application and I don't want to use razor, so I just need it to serve up HTML files (from the wwwroot folder), except for when the js calls my API controllers. In Web API 2 you could get the router to ignore HTML files so they are served directly e.g.

config.Routes.IgnoreRoute("Html", "{whatever}.html/{*pathInfo}");

similar to this example: http://www.strathweb.com/2014/04/ignoring-routes-asp-net-web-api/ is the IgnoreRoute functionality just not implemented or has it been changed?

At the moment if I have app.UseMvc(); in my Startup.cs any get request to "/" gets me this exception:

An unhandled exception occurred while processing the request.
InvalidOperationException: The view 'Index' was not found. The following     locations were searched:
/Views/Home/Index.cshtml
/Views/Shared/Index.cshtml.
Microsoft.AspNet.Mvc.Rendering.ViewEngineResult.EnsureSuccessful()

But when I leave it without MVC it serves up the index.html file when you request "/" - obviously my API controllers won't work then though.

like image 846
ecm_dev Avatar asked Jan 19 '15 02:01

ecm_dev


2 Answers

I think if you want to serve index.html even when your MVC option is enabled? If so you have to change one setting.

When you enable MVC there is a default route added to search for Home/Index when your url is like http://localhost:yourport.

When you disable MVC it will serve index.html as no route is present in that case.

So if you want to serve index.html when MVC is enabled then add the following in Configure function before using MVC.

app.UseDefaultFiles(new Microsoft.AspNet.StaticFiles.DefaultFilesOptions() { DefaultFileNames = new[] { "index.html" } });
// your UseMVC goes here.
like image 75
dotnetstep Avatar answered Oct 10 '22 02:10

dotnetstep


public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc();
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    app.UseDefaultFiles();
    app.UseStaticFiles();
    app.UseMvc();
} 
like image 37
lucas teles Avatar answered Oct 10 '22 00:10

lucas teles