ASP.NET MVC 默认 URL 视图

发布于 2024-08-17 07:39:38 字数 467 浏览 4 评论 0原文

我正在尝试将 MVC 应用程序的默认 URL 设置为应用程序区域内的视图。该区域称为“Common”,控制器为“Home”,视图为“Index”。

我尝试将 web.config 的表单部分中的 defaultUrl 设置为“~/Common/Home/Index”,但没有成功。

我还尝试在 global.asax 中映射新路线,因此:

routes.MapRoute(
        "Area",
        "{area}/{controller}/{action}/{id}",
        new { area = "Common", controller = "Home", action = "Index", id = "" }
    );

再次无济于事。

I'm trying to set the Default URL of my MVC application to a view within an area of my application. The area is called "Common", the controller "Home" and the view "Index".

I've tried setting the defaultUrl in the forms section of web.config to "~/Common/Home/Index" with no success.

I've also tried mapping a new route in global.asax, thus:

routes.MapRoute(
        "Area",
        "{area}/{controller}/{action}/{id}",
        new { area = "Common", controller = "Home", action = "Index", id = "" }
    );

Again, to no avail.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一口甜 2024-08-24 07:39:38

您列出的路由仅在明确输入 URL 时才有效:

yoursite.com/{area}/{controller}/{action}/{id}

该路由的含义是:

如果我收到具有有效 {area}、有效 {controller} 位于该区域,并且有效的 {action} 位于该控制器中,然后将其路由到那里。

您想要的是,如果他们只访问您的网站 yoursite.com,则默认为该控制器:

routes.MapRoute(
    "Area",
    "",
    new { area = "Common", controller = "Home", action = "Index" }
);

这表示如果他们不向 http://yoursite.com 附加任何内容 然后将其路由到以下操作:Common/Home/Index

另外,将其放在路由表的顶部。

确保您还让 MVC 知道注册应用程序中的区域:

将以下内容放入 Global.asax.cs 文件的 Application_Start 方法中:

AreaRegistration.RegisterAllAreas();

The route you listed only works if they explicitly type out the URL:

yoursite.com/{area}/{controller}/{action}/{id}

What that route says is:

If I get a request that has a valid {area}, a valid {controller} in that area, and a valid {action} in that controller, then route it there.

What you want is to default to that controller if they just visit your site, yoursite.com:

routes.MapRoute(
    "Area",
    "",
    new { area = "Common", controller = "Home", action = "Index" }
);

What this says is that if they don't append anything to http://yoursite.com then to route it to the following action: Common/Home/Index

Also, put it at the top of your routes table.

Makes sure you're also letting MVC know to register the areas you have in the application:

Put the following in your Application_Start method in the Global.asax.cs file:

AreaRegistration.RegisterAllAreas();
り繁华旳梦境 2024-08-24 07:39:38

你需要做的是:

  • 从global.asax.cs中删除默认路由

    //// 将在区域下创建默认路线图
    //路线.MapRoute(
    // 名称:“默认”,
    // url: "{controller}/{action}/{id}",
    // 默认值:new {controller = "Home", action = "Index", id = UrlParameter.Optional }
    //);
    
  • 更新 Common 区域中的 SecurityAreaRegistration.cs

  • 添加以下路由映射:

     context.MapRoute(
        “默认”,
        "",
        新{控制器=“主页”,操作=“索引”,id=“”}
    );
    

What you have to do is:

  • Remove Default Route from global.asax.cs

    //// default route map will be create under area
    //routes.MapRoute(
    //    name: "Default",
    //    url: "{controller}/{action}/{id}",
    //    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    //);
    
  • Update SecurityAreaRegistration.cs in area Common

  • Add following route mapping:

     context.MapRoute(
        "Default",
        "",
        new { controller = "Home", action = "Index", id = "" }
    );
    
深海不蓝 2024-08-24 07:39:38

你正在做的事情看起来是正确的。如果我不得不猜测,我会说这是由于您运行网站的方式而发生的。在 Visual Studio 中,如果您在按 F5 时选择了特定视图,则该视图将成为起始 Url - 尝试选择项目,然后按 F5?

What you're doing seems correct. If I had to guess I would say this is happening due to the way you are running your website. In Visual Studio, if you have a specific view selected when you hit F5 that view will be the starting Url - try selecting the Project and then hitting F5?

ゃ懵逼小萝莉 2024-08-24 07:39:38

在 Global.asax 中
删除 .MapRoute

并使其看起来像

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

在该区域的 yourareaAreaRegistration.cs 下(如果您通过 VS 添加该区域,则它就在那里)

public override void RegisterArea(AreaRegistrationContext context)
{
     //This is the key one         
     context.MapRoute("Root", "", new { controller = "Home", action = "Login" });

     //Add more MapRoutes if needed
}

In the Global.asax
delete the .MapRoute

and make it look like

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

then under the area's yourareaAreaRegistration.cs (its there if you added the area through VS)

public override void RegisterArea(AreaRegistrationContext context)
{
     //This is the key one         
     context.MapRoute("Root", "", new { controller = "Home", action = "Login" });

     //Add more MapRoutes if needed
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文