C#笔记:MVC4设置静态页为主页
- 2019 年 11 月 21 日
- 筆記
弄了一个小时,终于弄好了。
MVC的页面调度方式和一般不同。用的是路由规则。
第一步,需要添加路由MapPageRoute。
routes.MapPageRoute("Default_Page", "", "~/Views/Home/Default.html", true, null, new RouteValueDictionary { { "outgoing", new OutgoingConstraint() } });
这里会发现,这个路由规则还添加了限制,这个限制表明此规则只对入站的链接有效。这样可以避免Html.ActionLink这个方法生成的链接出问题。
第二步,OutgoingConstraint的代码,非常简单。在Global下面添加下面的代码。
public class OutgoingConstraint : IRouteConstraint { public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection) { return routeDirection == RouteDirection.IncomingRequest; } }
第三步,配置Webconfig。免得报错。
<compilation targetFramework="4.0" > <buildProviders> <add extension=".html" type="System.Web.Compilation.PageBuildProvider" /> </buildProviders> </compilation>
大功告成。打兔兔。