这个Global.asax路由设置有什么问题?

tis*_*hma 5 asp.net asp.net-routing url-routing

我可能期望从ASP.NET中获得太多,但在Apache中,重写URL非常简单,因此请求类似: http:// mysite/myfolder/mypage/niceurlparameter 实际上设法提供静态页面http://mysite/mypage.html

我如何在Global.asax中做到这一点?

我试过这个:

RouteTable.Routes.MapPageRoute("Static HTML", "myfolder/{page}/*", "~/myfolder/{page}.html");
Run Code Online (Sandbox Code Playgroud)

但是当我请求http:// mysite/myfolder/mypage/niceurlparameter时它会一直返回404 .

但是,这有效:

RouteTable.Routes.MapPageRoute("Static HTML", "myfolder/{page}.html/*", "~/myfolder/{page}.html");
Run Code Online (Sandbox Code Playgroud)

所以我在申请http://mysite/myfolder/mypage.html/niceurlparameter时会得到mypage.html.

我只是想摆脱我的网址中的".html"部分.我错过了什么?

更新:由于某种原因,在前一种情况下,'*'通配符尚未被接受.

改为:

RouteTable.Routes.MapPageRoute("Static HTML", "myfolder/{page}/{whatever}", "~/myfolder/{page}.html");
Run Code Online (Sandbox Code Playgroud)

似乎将请求路由到html页面,但后来我收到错误:

There is no build provider registered for the extension '.html'. 
Run Code Online (Sandbox Code Playgroud)

为什么在世界上它只适用于前一种情况(在URL中使用html),而不是在html被遗漏的情况下?

Par*_*rma 3

没有为扩展名“.html”注册构建提供程序

您收到此错误是因为静态 html 文件应由 IIS 直接处理。但是,在您的情况下,ASP.NET MVC 框架正在尝试处理 类型的文件.html,但它无法处理。

因此,如果您认为这是您需要做的,您将必须创建一个新的提供程序并在 web.config 文件中注册。看这个

ASP.NET 的自定义文件扩展名 - 需要帮助!

您只需将静态 html 内容更改为 .aspx 文件即可。简单的复制和粘贴就可以完成这项工作,并且应该可以正常工作。它将知道如何处理文件类型。