Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is wrong with this Global.asax routing setting?

I maybe expecting too much from ASP.NET, but in Apache it's trivial to rewrite urls so requesting something like: http://mysite/myfolder/mypage/niceurlparameter actually manages to serve static page http://mysite/mypage.html

How do I do that in Global.asax?

I've tried this:

RouteTable.Routes.MapPageRoute("Static HTML", "myfolder/{page}/*", "~/myfolder/{page}.html");

but it keeps returning 404 when i request http://mysite/myfolder/mypage/niceurlparameter.

However, this works:

RouteTable.Routes.MapPageRoute("Static HTML", "myfolder/{page}.html/*", "~/myfolder/{page}.html");

so I do get mypage.html when requesting http://mysite/myfolder/mypage.html/niceurlparameter.

I just want to get rid of ".html" part in my URLs. What am I missing?

UPDATE: For some reason, in former case the '*' wildcard has not been accepted.

Changing to:

RouteTable.Routes.MapPageRoute("Static HTML", "myfolder/{page}/{whatever}", "~/myfolder/{page}.html");

appears to route the request to html page, but then I get the error:

There is no build provider registered for the extension '.html'. 

Why in the world it would just work in the former case (with html in URL), and not when html is left out?

like image 549
tishma Avatar asked Apr 27 '12 08:04

tishma


People also ask

How do I register a route in global ASAX?

We register routes in the Global. asax file and we invoke a RegisterRoutes method in the Application_Start() method. Routing is used to create user friendly URLs. It can also be used to setup the startup page of the application, just like the ASP.NET Web Forms.

How to define routing in MVC?

In MVC, routing is a process of mapping the browser request to the controller action and return response back. Each MVC application has default routing for the default HomeController. We can set custom routing for newly created controller. The RouteConfig.

What is routing in MVC 5 with example?

Routing is a pattern matching system. Routing maps an incoming request (from the browser) to particular resources (controller & action method). This means routing provides the functionality to define a URL pattern that will handle the request. That is how the application matches a URI to an action.


Video Answer


1 Answers

There is no build provider registered for the extension '.html'

You receive this error because static html files should be handled by the IIS directly. However, in your case the ASP.NET MVC framework is trying to handle the file of type .html, which it can't.

So if you are think this is what you need to do you will have to make a new provider and register in web.config file. look at this

Custom file extensions for ASP.NET - help needed!

You could simply change your static html content to .aspx files. A simple copy and paste would do the job and it should work fine. It will know how to handle the file type.

like image 73
Parv Sharma Avatar answered Nov 15 '22 09:11

Parv Sharma