C# 如何在 asp.net MVC 4 & MVC 5 中设置默认控制器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14426526/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 11:46:01  来源:igfitidea点击:

How to set Default Controller in asp.net MVC 4 & MVC 5

c#asp.netasp.net-mvcasp.net-mvc-4asp.net-mvc-5

提问by Adrian10 BEN

How do I set Default Controller for my ASP.NET MVC4 project without making it HomeController?

如何为我的ASP.NET MVC4 项目设置默认控制器而不使其成为HomeController

How should I setup a default Areawhen the application starts?

应用程序启动时如何设置默认区域

采纳答案by Dave Alperovich

the best way is to change your route. The default route (defined in your App_Start) sets /Home/Index

最好的办法是改变你的路线。默认路由(在您的 App_Start 中定义)设置/Home/Index

routes.MapRoute(
        "Default", // Route name
        "{controller}/{action}/{id}", // URL with parameters*
        new { controller = "Home", action = "Index", 
        id = UrlParameter.Optional }
);

as the default landing page. You can change that to be any route you wish.

作为默认登陆页面。您可以将其更改为您希望的任何路线。

routes.MapRoute(
        "Default", // Route name
        "{controller}/{action}/{id}", // URL with parameters*
        new { controller = "Sales", action = "ProjectionReport", 
        id = UrlParameter.Optional }
);

回答by Amna Ali

I didnt see this question answered:

我没有看到这个问题的回答:

How should I setup a default Areawhen the application starts?

应用程序启动时如何设置默认区域

So, here is how you can set up a default Area:

因此,您可以通过以下方式设置默认区域:

var route = routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    ).DataTokens = new RouteValueDictionary(new { area = "MyArea" });

回答by Nimesh

Set below code in RouteConfig.csin App_Startfolder

App_Start文件夹中的RouteConfig.cs中设置以下代码

public static void RegisterRoutes(RouteCollection routes)
{
 routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
 routes.MapRoute(
 name: "Default",
 url: "{controller}/{action}/{id}",
 defaults: new { controller = "Account", action = "Login", id = UrlParameter.Optional });
}

IF still not working then do below steps

如果仍然无法正常工作,请执行以下步骤

Second Way :You simple follow below steps,

第二种方式:您只需按照以下步骤操作,

1) Right click on your Project

1)右键单击您的项目

2) Select Properties

2) 选择属性

3) Select Web option and then Select Specific Page (Controller/View) and then set your login page

3) 选择 Web 选项,然后选择特定页面(控制器/视图),然后设置您的登录页面

Here, Account is my controller and Login is my action method (saved in Account Controller)

这里,Account 是我的控制器,Login 是我的操作方法(保存在 Account Controller 中)

Please take a look attachedenter image description herescreenshot.

请看一下随附的在此处输入图片说明屏幕截图。

回答by Manjoor

In case you have only one controller and you want to access every action on root you can skip controller name like this

如果您只有一个控制器并且想要访问 root 上的每个操作,您可以像这样跳过控制器名称

routes.MapRoute(
        "Default", 
        "{action}/{id}", 
        new { controller = "Home", action = "Index", 
        id = UrlParameter.Optional }
);