What is wrong with this Global.asax routing setting?
Asked Answered
T

1

6

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?

Trying answered 27/4, 2012 at 8:43 Comment(3)
What version of IIS are you running? I seem to remember IIS 6 won't allow this out of the box, you need to add a wildcard mapping.Violence
See this so question for a possible solution: https://mcmap.net/q/1915851/-mvc3-map-routes-to-static-file-structure/25727Gelman
IIS is v7.5. The suggested link helped. ThanksTrying
N
3

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.

Niccolite answered 27/4, 2012 at 11:30 Comment(1)
I"m accepting this, as it actually resolves the issue. However, I'd never write anything custom that would eventually allow a web server to serve static files(!?!) URL rewriting is a way better path to take if you want static file redirection. IIS 7.5 and url rewrite 2 module are the way to go.Trying

© 2022 - 2024 — McMap. All rights reserved.