C# 在 Asp.Net MVC 中设置“主页”

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

Set "Homepage" in Asp.Net MVC

c#asp.net-mvcasp.net-mvc-routing

提问by NikolaiDante

In asp.net MVC the "homepage" (ie the route that displays when hitting www.foo.com) is set to Home/Index .

在 asp.net MVC 中,“主页”(即点击 www.foo.com 时显示的路由)设置为 Home/Index 。

  • Where is this value stored?
  • How can I change the "homepage"?
  • Is there anything more elegant than using RedirectToRoute() in the Index action of the home controller?
  • 这个值存储在哪里?
  • 如何更改“主页”?
  • 有什么比在家庭控制器的 Index 动作中使用 RedirectToRoute() 更优雅的吗?

I tried grepping for Home/Index in my project and couldn't find a reference, nor could I see anything in IIS (6). I looked at the default.aspx page in the root, but that didn't seem to do anything relevent.

我尝试在我的项目中搜索 Home/Index,但找不到参考,在 IIS (6) 中也看不到任何内容。我查看了根目录中的 default.aspx 页面,但这似乎没有做任何相关的事情。

Thanks

谢谢

采纳答案by Michael Stum

Look at the Default.aspx/Default.aspx.csand the Global.asax.cs

查看Default.aspx/Default.aspx.cs和 Global.asax.cs

You can set up a default route:

您可以设置默认路由:

        routes.MapRoute(
            "Default", // Route name
            "",        // URL with parameters
            new { controller = "Home", action = "Index"}  // Parameter defaults
        );

Just change the Controller/Action names to your desired default. That should be the last route in the Routing Table.

只需将控制器/操作名称更改为您想要的默认值。那应该是路由表中的最后一条路由。

回答by Micha? Chaniewski

check RegisterRoutes method in global.asax.cs - it's the default place for route configuration...

检查 global.asax.cs 中的 RegisterRoutes 方法 - 它是路由配置的默认位置...

回答by JTW

ASP.NET Core

ASP.NET 核心

Routing is configured in the Configuremethod of the Startupclass. To set the "homepage" simply add the following. This will cause users to be routed to the controller and action defined in the MapRoute method when/if they navigate to your site's base URL, i.e., yoursite.com will route users to yoursite.com/foo/index:

路由是在类的Configure方法中配置的Startup。要设置“主页”,只需添加以下内容。这将导致用户在/如果导航到您站点的基本 URL 时被路由到 MapRoute 方法中定义的控制器和操作,即 yoursite.com 将用户路由到 yoursite.com/foo/index:

app.UseMvc(routes =>
{
   routes.MapRoute(
   name: "default",
   template: "{controller=FooController}/{action=Index}/{id?}");
});

Pre-ASP.NET Core

前 ASP.NET 核心

Use the RegisterRoutes method located in either App_Start/RouteConfig.cs (MVC 3 and 4) or Global.asax.cs (MVC 1 and 2) as shown below. This will cause users to be routed to the controller and action defined in the MapRoute method if they navigate to your site's base URL, i.e., yoursite.com will route the user to yoursite.com/foo/index:

使用位于 App_Start/RouteConfig.cs(MVC 3 和 4)或 Global.asax.cs(MVC 1 和 2)中的 RegisterRoutes 方法,如下所示。如果用户导航到您站点的基本 URL,这将导致用户被路由到 MapRoute 方法中定义的控制器和操作,即 yoursite.com 会将用户路由到 yoursite.com/foo/index:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    // Here I have created a custom "Default" route that will route users to the "YourAction" method within the "FooController" controller.
    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "FooController", action = "Index", id = UrlParameter.Optional }
    );
}

回答by Niraj

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",               
            defaults: new { controller = "Your Controller", action = "Your Action", id = UrlParameter.Optional }
        );
    }
}

回答by vickey1611

I tried the answer but it didn't worked for me. This is what i ended up doing:

我尝试了答案,但对我不起作用。这就是我最终做的:

Create a new controller DefaultController. In index action, i wrote one line redirect:

创建一个新的控制器 DefaultController。在索引操作中,我写了一行重定向:

return Redirect("~/Default.aspx")

In RouteConfig.cs, change controller="Default" for the route.

在 RouteConfig.cs 中,更改路由的 controller="Default"。

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

回答by Ankur Shah

Step 1: Click on Global.asax File in your Solution.

步骤 1:单击解决方案中的 Global.asax 文件。

Step 2: Then Go to Definition of

第 2 步:然后转到定义

RouteConfig.RegisterRoutes(RouteTable.Routes);

RouteConfig.RegisterRoutes(RouteTable.Routes);

Step 3: Change Controller Name and View Name

步骤 3:更改控制器名称和视图名称

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

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

回答by Chris Gong

Attribute Routing in MVC 5

MVC 5 中的属性路由

Before MVC 5 you could map URLs to specific actions and controllers by calling routes.MapRoute(...)in the RouteConfig.cs file. This is where the url for the homepage is stored (Home/Index). However if you modify the default route as shown below,

在 MVC 5 之前,您可以通过调用routes.MapRoute(...)RouteConfig.cs 文件将 URL 映射到特定的操作和控制器。这是存储主页 url 的位置 ( Home/Index)。但是,如果您修改默认路由,如下所示,

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

keep in mind that this will affect the URLs of other actions and controllers. For example, if you had a controller class named ExampleControllerand an action method inside of it called DoSomething, then the expected default url ExampleController/DoSomethingwill no longer work because the default route was changed.

请记住,这会影响其他操作和控制器的 URL。例如,如果您有一个名为 的控制器类,并且其中有一个名为ExampleController的操作方法DoSomething,那么预期的默认 urlExampleController/DoSomething将不再起作用,因为默认路由已更改。

A workaround for this is to not mess with the default route and create new routes in the RouteConfig.cs file for other actions and controllers like so,

解决方法是不要弄乱默认路由,并在 RouteConfig.cs 文件中为其他操作和控制器创建新路由,例如,

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
    name: "Example",
    url: "hey/now",
    defaults: new { controller = "Example", action = "DoSomething", id = UrlParameter.Optional }
);

Now the DoSomethingaction of the ExampleControllerclass will be mapped to the url hey/now. But this can get tedious to do for every time you want to define routes for different actions. So in MVC 5 you can now add attributes to match urls to actions like so,

现在类的DoSomething动作ExampleController将被映射到 url hey/now。但是每次你想为不同的动作定义路由时,这样做会变得很乏味。因此,在 MVC 5 中,您现在可以添加属性以将 url 与这样的操作相匹配,

public class HomeController : Controller
{
    // url is now 'index/' instead of 'home/index'
    [Route("index")]
    public ActionResult Index()
    {
        return View();
    }
    // url is now 'create/new' instead of 'home/create'
    [Route("create/new")]
    public ActionResult Create()
    {
        return View();
    }
}

回答by Martin Kei

If you don't want to change the router, just go to the HomeController and change MyNewViewHere in the index like this:

如果您不想更改路由器,只需转到 HomeController 并更改索引中的 MyNewViewHere,如下所示:

    public ActionResult Index()
    {
        return View("MyNewViewHere");
    }