文章内容

2017/3/12 11:24:45,作 者: 黄兵

设置ASP.NET MVC站点默认页为html页

更新:上周的解决方法在Area=""使用默认路由映射时会出问题,见解决方法二

 

今天部署了一个Asp.NET MVC站点,希望它的默认页是一个html页,在vs2010中给站点根目录增加了default.html,然后调用没有什么问题,但部署到IIS7上,就是不起作用,试了routes.IgnoreRoute,但还是访问原来的路由设置的controller。

 

上网搜索了一下,找到下面两种解决的方法:(但我感觉都不好,最后自己找了一个解决的方法,感觉还行)

方法1:

在Global.asax文件中增加

protected void Application_BeginRequest(object sender, EventArgs e)
{
    if (Context.Request.FilePath == "/") Context.RewritePath("Default.html");
}

方法2:

新建一个路由DefaultController,并把它设置为默认路由,在Action中增加

Redirect(Url.Content("~/Default.html"));

 

我的方法:

      使用Web窗体应用程序路由MapPageRoute,开始时还碰到点问题。我增加routes.MapPageRoute("Default_Page", "/", "~/Default.html"),iis报错——路由 URL 不能以“/”或“~”字符开头,并且不能包含“?”字符 。要修改为routes.MapPageRoute("Default_Page", "", "~/default.html");(注意如果你的默认页是aspx页面,将default.html修改为default.aspx,到这里就应该可以工作了)。这时iis报错变为——"没有为扩展名“.html”注册的生成提供程序。可以在 machine.config 或 web.config 中的 <compilation><buildProviders> 节注册一个。 ",这说明路由设置已经起了作用,看来是.html没有找到处理程序,哈哈,离成功不远了。

    在Web.config文件中的<compilation>节点中增加:

 

     <buildProviders>
        <add extension=".htm" type="System.Web.Compilation.PageBuildProvider" />
      </buildProviders>

 

设置为其他文件可以依此类推。wangzhi

 

2011-04-25更新:  wangzhi

解决方法二:

1)站点根目录增加了default.html;

2)修改Global.asax默认的路由注册,去掉默认controller:

routes.MapRoute(
                "Default", // 路由名称
                "{controller}/{action}/{id}", // 带有参数的 URL
                new { controller = "Home",  action = "Index", id = UrlParameter.Optional } // 参数默认值
            );

 

routes.MapRoute(
                "Default", // 路由名称
                "{controller}/{action}/{id}", // 带有参数的 URL
                new { action = "Index", id = UrlParameter.Optional } // 参数默认值
            );

 

 

将iis中的默认文档配置为default.html

转载自:CSDN-aoyo

分享到:

发表评论

评论列表