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>

大功告成。打兔兔。