MVC4 - Is there a way to route the root to a “norm

2019-04-05 08:04发布

问题:

I have an MVC4 app, but I'm primarily using it for the WebAPI parts. I want to have a "plain old HTML" file sent back to the user (which will then use KnockoutJS or KendoUI to pull JSON from the webapi controllers).

I know I can do this:

routes.IgnoreRoute("{page}.html");

And then if I browse to "localhost/index.html" it does successfully just return my .html page. However, I really want to map the "root" default path "localhost/" to return my index.html.

I tried this:

routes.MapPageRoute("root", "", "~/index.html");

but that throws the error:

There is no build provider registered for the extension '.html'. You can register one in the <compilation><buildProviders> section in machine.config or web.config. Make sure is has a BuildProviderAppliesToAttribute attribute which includes the value 'Web' or 'All'.

Anyone have any ideas on how I can make this work? I could just hit a default controller that returns a plain html page, but it seems like overkill to go through the entire ASP.NET stack once for the HTML page, which then just calls a WebAPI URL to go back through the ASP.NET stack to get some JSON data for the page's model.

I basically just want to "skip" all the MVC plumbing and have IIS send me back the html page, as if it wasn't an ASP.NET app, or at least do as little processing as possible.

回答1:

Add the following to your routing config:

routes.IgnoreRoute("");