在Asp.net中设置默认页面

有没有任何部分或代码,允许我们在web.config设置默认页面?

例如,当人们第一次访问我的网站时,我希望他们看到CreateThing.aspx而不是Default.aspx

我知道的解决scheme:

  1. 把这行代码=> Response.Redirect("CreateThings.aspx")放在Default.aspx Page_Load事件中,但是这个方法真的很天真。

  2. 我们可以使用IIS (默认页面configuration),但是我想在Asp.NET应用程序上做同样的事情。

如果你知道一个更好的解决scheme,请让我知道。

提前致谢。

PS:我问Web.Config的原因是我相信Web.Config会帮助我们通过这个。

编辑 :我只是想知道如何微软.NET团队忘了这么简单的事情:)

编辑:

这可能是现在的另一个解决scheme:

 <defaultDocument> <files> <clear /> <add value="Default.aspx" /> <add value="Default.htm" /> <add value="Default.asp" /> <add value="index.htm" /> <add value="index.html" /> <add value="iisstart.htm" /> </files> </defaultDocument> 

如果使用IIS 7或IIS 7.5,则可以使用

 <system.webServer> <defaultDocument> <files> <clear /> <add value="CreateThing.aspx" /> </files> </defaultDocument> </system.webServer> 

http://www.iis.net/ConfigReference/system.webServer/defaultDocument

提示#84:你知道吗…如何在Visual Web Developer中为你的网站设置一个起始页面?

只需右键点击你想成为起始页面的页面,并说“设置为起始页面”。

将default.aspx映射为HttpHandler路由,并从HttpHandler中redirect到CreateThings.aspx。

 <add verb="GET" path="default.aspx" type="RedirectHandler"/> 

确保Default.aspx在您的应用程序根目录中不存在 。 如果它在物理上存在,HttpHandler将不会被赋予执行的机会。 物理文件覆盖HttpHandler映射。

此外,您可以重新使用此页面以外的其他页面default.aspx。

 <add verb="GET" path="index.aspx" type="RedirectHandler"/> 

你的App_Code中的//RedirectHandler.cs

 using System; using System.Collections.Generic; using System.Linq; using System.Web; /// <summary> /// Summary description for RedirectHandler /// </summary> public class RedirectHandler : IHttpHandler { public RedirectHandler() { // // TODO: Add constructor logic here // } #region IHttpHandler Members public bool IsReusable { get { return true; } } public void ProcessRequest(HttpContext context) { context.Response.Redirect("CreateThings.aspx"); context.Response.End(); } #endregion } 

如果您使用表单身份validation,您可以尝试下面的代码:

 <authentication mode="Forms"> <forms name=".FORM" loginUrl="Login.aspx" defaultUrl="CreateThings.aspx" protection="All" timeout="30" path="/"> </forms> </authentication> 

如果您在您的网站上使用login页面,请转到web.config文件

 <authentication mode="Forms"> <forms loginUrl="login.aspx" defaultUrl="index.aspx" > </forms> </authentication> 

将您的身份validation标签replace为上面(其中index.aspx将成为您的启动页面)

还有一件事是在你的web.config文件里面写的

 <configuration> <system.webServer> <defaultDocument> <files> <clear /> <add value="index.aspx" /> </files> </defaultDocument> </system.webServer> <location path="index.aspx"> <system.web> <authorization> <allow users="*" /> </authorization> </system.web> </location> </configuration> 

您可以使用web.config覆盖IIS默认文档设置

 <system.webServer> <defaultDocument> <files> <clear /> <add value="DefaultPageToBeSet.aspx" /> </files> </defaultDocument> </system.webServer> 

或者使用IIS,请参阅链接以供参考http://www.iis.net/configreference/system.webserver/defaultdocument

我更喜欢使用以下方法:

 system.webServer> <defaultDocument> <files> <clear /> <add value="CreateThing.aspx" /> </files> </defaultDocument> </system.webServer> 

我已经完成了所有上述解决scheme,但没有奏效。

我的默认页面不是一个aspx页面,它是一个html页面。

这篇文章解决了这个问题。 https://weblog.west-wind.com/posts/2013/aug/15/iis-default-documents-vs-aspnet-mvc-routes

基本上,在我的\ App_Start \ RouteConfig.cs文件中,我不得不添加一行:

 public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.IgnoreRoute(""); // This was the line I had to add here! routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); } 

希望这可以帮助别人,我花了很长时间才find答案。