asp.net-mvc 如何在 ASP.NET MVC 应用程序中设置用于调试的启动页?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1333002/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you set the startup page for debugging in an ASP.NET MVC application?
提问by Ante
How do you start debugging the application at the application root? For example: http://localhost:49742/
如何在应用程序根目录下开始调试应用程序?例如:http://localhost:49742/
I'm always getting a page which doesn't exist, such as: http://localhost:49742/Views/Home/About.aspx
我总是得到一个不存在的页面,例如: http://localhost:49742/Views/Home/About.aspx
Note that it would be OK to start at http://localhost:49742/Views/Home/About
回答by Mark Seemann
Go to your project's properties and set the start page property.
转到项目的属性并设置起始页属性。
- Go to the project's Properties
- Go to the Webtab
- Select the Specific Pageradio button
- Type in the desired url in the Specific Pagetext box
- 转到项目的属性
- 转到Web选项卡
- 选择特定页面单选按钮
- 在特定页面文本框中输入所需的 url
回答by 1c1cle
While you can have a default page in the MVC project, the more conventional implementation for a default view would be to use a default controller, implememented in the global.asax, through the 'RegisterRoutes(...)' method. For instance if you wanted your Public\Home controller to be your default route/view, the code would be:
虽然您可以在 MVC 项目中有一个默认页面,但默认视图的更传统的实现是使用默认控制器,通过 'RegisterRoutes(...)' 方法在 global.asax 中实现。例如,如果您希望您的 Public\Home 控制器成为您的默认路由/视图,代码将是:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Public", action = "Home", id = UrlParameter.Optional } // Parameter defaults
);
}
For this to be functional, you are required to have have a set Start Page in the project.
要使其正常运行,您需要在项目中有一个设置的起始页。
回答by Greg Gum
This works for me under Specific Page for MVC:
这在MVC的特定页面下对我有用:
/Home/Index
Update: Currently, I just use a forward slash in the "Specific Page" textbox, and it takes me to the home page as defined in the routing:
更新:目前,我只在“特定页面”文本框中使用正斜杠,它会将我带到路由中定义的主页:
/
回答by cihata87
Selecting a specific page from Project properties does not solve my problem.
从项目属性中选择特定页面并不能解决我的问题。
In MVC 4 open App_Start/RouteConfig.cs
在 MVC 4 中打开 App_Start/RouteConfig.cs
For example, if you want to change startup page to Login:
例如,如果要将启动页面更改为Login:
routes.MapRoute(
"Default", // Route name
"", // URL with parameters
new { controller = "Account", action = "Login"} // Parameter defaults
);
回答by RichardOD
If you want to start at the "application root" as you describe right click on the top level Default.aspx page and choose set as start page. Hit F5 and you're done.
如果您想按照您的描述从“应用程序根目录”开始,请右键单击顶级 Default.aspx 页面并选择设置为开始页面。按F5,你就完成了。
If you want to start at a different controller action see Mark's answer.
如果您想从不同的控制器操作开始,请参阅 Mark 的回答。
回答by Syd
Revisiting this page and I have more information to share with others.
重新访问此页面,我有更多信息要与他人分享。
Debugging environment (using Visual Studio)
调试环境(使用 Visual Studio)
1a) Stephen Walter's link to set the startup page on MVC using the project properties is only applicable when you are debugging your MVC application.
1a) Stephen Walter 使用项目属性在 MVC 上设置启动页面的链接仅在您调试 MVC 应用程序时适用。
1b) Right mouse click on the .aspx page in Solution Explorer and select the "Set As Start Page" behaves the same.
1b) 在解决方案资源管理器中的 .aspx 页面上单击鼠标右键,然后选择“设置为起始页面”的行为相同。
Note: in both the above cases, the startup page setting is only recognised by your Visual Studio Development Server. It is not recognised by your deployed server.
注意:在上述两种情况下,启动页面设置只能被您的 Visual Studio 开发服务器识别。您部署的服务器无法识别它。
Deployed environment
部署环境
2a) To set the startup page, assuming that you have not change any of the default routings, change the content of /Views/Home/Index.aspx to do a "Server.Transfer" or a "Response.Redirect" to your desired page.
2a) 要设置启动页面,假设您没有更改任何默认路由,将 /Views/Home/Index.aspx 的内容更改为您想要的“Server.Transfer”或“Response.Redirect”页。
2b) Change your default routing in your global.asax.cs to your desired page.
2b) 将 global.asax.cs 中的默认路由更改为所需页面。
Are there any other options that the readers are aware of? Which of the above (including your own option) would be your preferred solution (and please share with us why)?
还有其他读者知道的选择吗?以上哪个(包括您自己的选择)将是您的首选解决方案(请与我们分享原因)?

