简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM