如何设置ASP.NET Entity Framework项目启动默认显示WebForm1
设置ASP.NET Web Forms启动默认显示WebForm1的方法
你用空模板创建的Web Forms项目默认没有配置路由,要设置启动时优先显示WebForm1,有几种直接可行的方法:
方法一:通过Visual Studio快速设置起始页
- 在解决方案资源管理器中找到
WebForm1.aspx文件 - 右键点击该文件,选择设为起始页
- 之后每次调试启动项目,都会自动打开这个页面,适合开发阶段快速配置
方法二:修改Web.config配置默认文档(部署后仍生效)
如果需要部署到服务器后也保持这个默认页规则,要修改项目根目录的Web.config文件:
- 找到或添加
<system.webServer>节点 - 在节点内添加默认文档配置,指定
WebForm1.aspx为默认打开页面:
<system.webServer> <defaultDocument> <files> <clear /> <!-- 清空原有默认文档列表,避免冲突 --> <add value="WebForm1.aspx" /> </files> </defaultDocument> </system.webServer>
可选:通过路由配置默认页(适合复杂URL场景)
如果需要用路由规则来指定默认页,需手动添加路由配置:
- 在项目根目录的
Global.asax文件中,找到Application_Start方法,添加路由注册代码:
void Application_Start(object sender, EventArgs e) { RouteConfig.RegisterRoutes(RouteTable.Routes); }
- 在App_Code目录下创建
RouteConfig.cs类,配置默认路由指向WebForm1:
public class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { // 配置根路径默认跳转至WebForm1.aspx routes.MapPageRoute("", "", "~/WebForm1.aspx"); } }
内容的提问来源于stack exchange,提问作者icecocotea
相关产品推荐
相关产品推荐

