在asp.net mvc 启动时,哪个网页定为首页,这个问题网上有很多种解决方案,在此处整理一下。
方案一:
具体详情看图。
方案二:
不用上述方案,在代码中完成:
在mvc 下的Global.asax.cs下编写:
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
RegisterRoutes(RouteTable.Routes);
}
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // 路由名称
"{controller}/{action}/{id}", // 带有参数的 URL
new { controller = "User", action = "Index", id = UrlParameter.Optional } // 参数默认值
);
}
}
我这里网站首页是一个登录页面,也就是login.cshtml,默认跳转到index页面,不过index页面要登录权限所以先跳转到登录页面。
方案三:
在Global.asax文件中增加:
protected void Application_BeginRequest(object sender, EventArgs e)
{
if (Context.Request.FilePath == "/") Context.RewritePath("index.html");
}
方案四:
新建一个路由DefaultController,并把它设置为默认路由,在Action中增加Redirect(Url.Content("~/index.html"));此方法需要修改web.config配置在Web.config文件中的<compilation>节点中增加:
<buildProviders>
<add extension=".htm" type="System.Web.Compilation.PageBuildProvider" />
</buildProviders>
本文汇总了在ASP.NET MVC中设置首页的四种常见方法:通过CSDN博客分享的方案、在Global.asax.cs中配置路由、利用Application_BeginRequest事件重写路径以及创建默认控制器并设置路由。每个方案都有其适用场景,如考虑登录权限或自定义首页内容。
807

被折叠的 条评论
为什么被折叠?



