【问题标题】:Rounting in asp mvc areas - issue with more areasasp mvc 区域中的路由 - 更多区域的问题
【发布时间】:2012-01-10 20:38:06
【问题描述】:

路由让我在这几天发疯 :( 我有两个区域:CityPage 和 Job 在城市页面上,我有一个导航到工作区域的链接列表。 这是 CityPage 的操作和路由代码:

public ActionResult Index(string city, string countryCode)
        {
            return View();
        }

In CityPageAreaRegistration.cs class:
context.MapRoute(
                null,
                "CityPage/{countryCode}/{city}",
                new { area = "CityPage", controller = "Home", action = "Index", city = "" }
            );

我在这里得到的网址很好

http://localhost/CityPage/UK/London

我可以从 CityPage 索引导航到工作列表 (Job/Home/Index)

@Html.ActionLink("Jobs", "Index", "Home", new { area = "Job" }, null)

这是来自工作区的代码:

public ActionResult Index()
        {
            return View();
        }
context.MapRoute(
                null,
                "{area}/{countryCode}/{city}/Job/{controller}/{action}",
                new { area = "Job", controller = "Home", action = "Index",
                      countryCode = "UK",
                      city = "London"
                }
            );

我在这里得到的 URL 还是可以的

http://localhost/CityPage/UK/London/Job

但在那个页面上,我有指向工作详细信息的链接 (Job/Home/Show)

@Html.ActionLink("Job Example", "Show", "Home", new { area = "Job", jobId = 8765 }, null)

我得到的网址是

http://localhost/CityPage/UK/London/Job/Home/Show?jobId=8765

I have tried to add routing like this

            context.MapRoute(
                "Jobs",
                "{area}/Job/{jobId}",
                new
                {
                    area = "Job",
                    controller = "Home",
                    action = "Index",
                    countryCode = "RS",
                    city = "Kragujevac",
                    jobId = ""
                }
            );

但它不起作用。我不知道我在做什么错:( 我想要获取的 URL 类似于

http://localhost/CityPage/UK/London/Job/8765

我还在学习路由。但是领域让我...
我没有向 Global.asax 添加任何路由我认为我需要在区域 routing.cs 类中编写路由代码,对吗?

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-3 asp.net-mvc-routing asp.net-mvc-areas


    【解决方案1】:

    看起来你已经很接近了。

    我创建了这样的路线:

    context.MapRoute(
                "Job_Locator",
                "{countryCode}/{city}/Job/{jobId}",
                new
                {
                    area = "Job",
                    controller = "Home",
                    action = "Show"
                }
            );
    

    然后是这样的操作链接:

    @Html.ActionLink("Test", "Index", "Home", new { Area = "Job", countryCode = "UK", city = "London", jobId = 10 }, null)
    

    动作在哪里:

    public ActionResult Index(string city, string countryCode, int jobId)
    {
        return View();
    }
    

    请记住,我创建了一个名为“Job”的新区域,并且在那里我有一个 HomeController 和 Index 操作

    【讨论】:

    • 我在 JobAreaRegistration.cs 中添加了这个路由代码,但我再次得到相同的 URL localhost/CityPage/UK/London/Job/Home/Show?jobId=8765
    • 你添加了我写的动作吗?路由需要与 Action 匹配才能摆脱查询字符串。
    • 是的,我在你写的时候添加了动作方法和链接和路由:(我调试应用程序,我得到了所有的值,我不知道为什么 URL 是错误的......在你的例子中你叫“来自链接的索引”,我正在尝试调用“显示”。这里的有线事情是当我调用“索引”时 URL 是:localhost:65377/UK/London/Job/10
    • 确保重新启动 asp.net 开发服务器(托盘中的图标向下),以便它获取新的路由信息​​。我唯一能想到的可能是弄乱了您的路线,除此之外,如果您的 CityPageAreaRegistration 正在做一些时髦的事情,或者您在我给您的新路线之前有默认路线。
    • 哈哈..我找到了。谢谢你的代码很好我复制它并调用“索引”我将路由操作更改为显示并且它有效。谢谢:)
    【解决方案2】:

    我一直在关注你的疯狂。我想建议你尝试一种叫做 T4MVC 的东西。它以 NuGet 包的形式提供,我认为现在是 MvcContrib 的一部分。

    我是 T4MVC 的忠实粉丝。它使将 URL 路由到操作方法变得更加容易,并且摆脱了许多魔术字符串。

    这是我们应用程序“公共”区域中的路由定义示例:

    context.MapRoutes(null,
        new[] { string.Empty, "features", "features/{version}" },
        new
        {
            area = MVC.Common.Name,
            controller = MVC.Common.Features.Name,
            action = MVC.Common.Features.ActionNames.ForPreview,
            version = "december-2011-preview-2",
        },
        new { httpMethod = new HttpMethodConstraint("GET") }
    );
    

    请注意,区域、控制器或动作名称没有魔法字符串。所有这些东西都是由 T4MVC 强类型化的。最重要的是:看看我们如何生成指向这些页面的链接:

    @Html.ActionLink("December 2011 Preview 2 features", 
        MVC.Common.Features.ForPreview())
    @Html.ActionLink("December 2011 Preview 1 features", 
        MVC.Common.Features.ForPreview("december-2011-preview-1"))
    

    有一些带有 ActionResult 参数的 ActionLink 重载。 T4MVC 为您提供了一个强类型的 ActionResult 方法,您可以在此重载中使用该方法。传递给方法的参数将映射到您的路由定义。

    当我编写路由和操作方法时,我只担心路由中的 URL 参数的名称与它们映射到的操作方法的参数匹配。所以这里是 FeaturesController 上 ForPreview 方法的签名:

    public virtual ActionResult ForPreview(
        string version = "december-2011-preview-2")
    

    这种方法使区域之间的交叉链接变得更加容易,试一试。

    更新

    糟糕,上面的路由定义默认不会编译。我忘了我在项目中有这个扩展方法:

    public static void MapRoutes(this AreaRegistrationContext context,
        string name, IEnumerable<string> urls, object defaults, 
        object constraints = null)
    {
        foreach (var url in urls)
            context.MapRoute(name, url, defaults, constraints);
    }
    

    【讨论】:

      猜你喜欢
      • 2011-11-17
      • 1970-01-01
      • 1970-01-01
      • 2012-08-03
      • 1970-01-01
      • 2012-02-21
      • 2016-08-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多