如何在 ASP.NET MVC 5 中设置指向同一控制器的 2 个路由

发布于 2025-01-19 18:21:13 字数 1340 浏览 5 评论 0 原文

我有一个ASP.NET MVC 5网站,该网站具有控制器 myFirstController

这样,我可以使用 https://example.server.com/myfirst 加载索引视图。

另一方面,我还有另一种视图,该视图呈现链接(html a标签的href属性):

Url.Action("Index", "MyFirst")

该URL渲染会导致链接生成:

https://example.server.com/MyFirst 

到目前为止,太好了。现在,我需要有一个类似的路由

https://example.server.com/MySecond

,即URL应该加载相同的 index myFirst 控制器的视图,而无需创建名为 mySecondController 的新控制器。这就像拥有一个 myFirst 控制器别名名为 mysecond

ruteconfig.cs 文件中,我添加了以下内容:

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

我对此代码遇到的问题是,我在其他视图中具有此渲染代码:

@if (somecondition)
{
    ... Url.Action("Index", "MyFirst") ...
}
else
{
    ... Url.Action("Index", "MySecond") ...
}

我需要,如果 somecondition 是正确的,渲染的URL为 https://example.server.com/myfirst ,如果是false,则渲染的URL为 https://example.server.server.server.com/mysecond

我遇到的问题是,两者都被渲染为 https://example.server.com/mysecond

我该如何完成这项任务?

I have an ASP.NET MVC 5 web site which has a controller MyFirstController.

That way, I can load the index view by using https://example.server.com/MyFirst.

On the other hand, I have another view that renders the link (HREF attribute of HTML A tag) this way:

Url.Action("Index", "MyFirst")

That URL rendering causes the link to be generated:

https://example.server.com/MyFirst 

So far, so good. Now, I need to have a route something like this

https://example.server.com/MySecond

That URL should load the same Index view of MyFirst controller without creating a new controller named MySecondController. This is like having a MyFirst controller alias named MySecond.

In RouteConfig.cs file I added this:

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

The problem I have with this code is that I have this rendering code in the other view:

@if (somecondition)
{
    ... Url.Action("Index", "MyFirst") ...
}
else
{
    ... Url.Action("Index", "MySecond") ...
}

I need that if somecondition is true, the rendered URL to be https://example.server.com/MyFirst and if it is false, the rendered URL to be https://example.server.com/MySecond.

The problem I am having is that both are being rendered as https://example.server.com/MySecond.

How can I accomplish this task?

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

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

发布评论

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

评论(5

別甾虛僞 2025-01-26 18:21:13

而不是使用 url.action ,使用 url.route ,它允许解释传递申请渲染URL的路由的名称。

要渲染/mysecond url,请传递 mysecond 路由的名称。

@Url.RouteUrl("MySecond")

做同样的事情以渲染/myFirst url,但是由于它没有明确的路由名称,您必须同时传递默认路由的名称和的名称myfirst 控制器。

@Url.RouteUrl("Default", new { Controller = "MyFirst" })

您可以省略 index 操作的名称,因为已将其设置为默认一个。

Instead of using Url.Action, use Url.Route as it allows to explictly pass the name of the route to apply for rendering the url.

To render the /MySecond url, pass the name of the MySecond route.

@Url.RouteUrl("MySecond")

Do the same to render the /MyFirst url, but since it doesn't have an explicit route name, you'll have to pass both the name of the default route and the name of the MyFirst controller.

@Url.RouteUrl("Default", new { Controller = "MyFirst" })

You can omit the name of the Index action, as it has been set up as the default one.

ゝ杯具 2025-01-26 18:21:13

自定义路由应在默认路由前。

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

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

Custom route should be before default route.

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

routes.MapRoute(
                "Default",                                              
                "{controller}/{action}/{id}",                           
                new { controller = "Home", action = "Index", id = "" }  
            );
那些过往 2025-01-26 18:21:13

在控制器中尝试使用

[Route("MyFirst")]
[Route("MySecond")]
public class MyFirstController : Controller
{
  
}

In the controller try using

[Route("MyFirst")]
[Route("MySecond")]
public class MyFirstController : Controller
{
  
}
泛泛之交 2025-01-26 18:21:13

而不是使用url.Action使用 url.route 。此方法可以选择提供路由名称,并可以选择操作和控制器。

if (somecondition)
{
    @Url.RouteUrl("Default", new { Controller = "MyFirst" }) //Action "Index" will be defaulted so do not need to provide it
}
else
{
    Url.RouteUrl("MySecond");
}

您还可以在URLHELPER上创建扩展方法,以便您没有散落的“魔术字符串”的视图:

namespace MyWebsite.Helpers
{
    public static class UrlHelperExtension
    {
        public static string MyFirst(this UrlHelper url)
        {
            return url.RouteUrl("Default", new { Controller = "MyFirst" });
        }

        public static string MySecond(this UrlHelper url)
        {
            return url.RouteUrl("MySecond");
        }
    }
}

然后您可以使用@url.myfirst()@url.mysecond ()

Instead of using Url.Action use Url.Route. This method has the option to provide the route name and optionally the action and controller.

if (somecondition)
{
    @Url.RouteUrl("Default", new { Controller = "MyFirst" }) //Action "Index" will be defaulted so do not need to provide it
}
else
{
    Url.RouteUrl("MySecond");
}

You could also create extension methods on UrlHelper so that you don't have "magic strings" littered through out your views:

namespace MyWebsite.Helpers
{
    public static class UrlHelperExtension
    {
        public static string MyFirst(this UrlHelper url)
        {
            return url.RouteUrl("Default", new { Controller = "MyFirst" });
        }

        public static string MySecond(this UrlHelper url)
        {
            return url.RouteUrl("MySecond");
        }
    }
}

Then you can use @Url.MyFirst() or @Url.MySecond()

娇女薄笑 2025-01-26 18:21:13

您可以将2个路由设置为相同的控制器,如下所示:

[Route("first_route")]
[Route("second_route")]
public class MyController : Controller{

}

You can set 2 route point to the same controller like shown below:

[Route("first_route")]
[Route("second_route")]
public class MyController : Controller{

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