【问题标题】:AspNetMvc appends url when navigating between two different routesAspNetMvc 在两条不同的路由之间导航时附加 url
【发布时间】:2018-04-01 19:49:54
【问题描述】:

我的 mvc 站点中有一个名为 adpan 的区域,其路由配置如下:

context.MapRoute(
     "adpan_clinics",
     "adpan/DoctorClinics/{doctorObj}/{controller}/{action}/{clinicObj}",
     new { action = "Index", Controller = "Clinics", clinicObj = UrlParameter.Optional }
     );

context.MapRoute(
    "adpan_default",
    "adpan/{controller}/{action}/{obj}",
    new { action = "Index", Controller = "Dashboard", obj = UrlParameter.Optional }
    );

我使用 T4MVC 进行路由。一切正常,直到我想使用操作链接从 adpan_clinics 路由回 adpan_default

场景)我有以下网址:

第一个网址) http://localhost/adpan/Doctors/Index

第二个网址) http://localhost/adpan/DoctorClinics/doctor1/Clinics/index

我可以通过 1st url 的 视图上的操作链接重定向到 2nd url,如下所示:

 @Html.ActionLink("Manage Clinics", MVC.adpan.Clinics.Index().AddRouteValues(new { doctorObj = item.Url }))

但是,当使用以下操作链接重定向回 第一个 url 时,我遇到了 url 附加问题:

 @Html.ActionLink("Back to Doctors", MVC.adpan.Doctors.Index())

此操作链接为我提供了以下 错误 url 而不是 第一个 url(尽管页面加载正确!):

错误的网址) http://localhost/adpan/DoctorClinics/doctor1/Doctors

注意:我也尝试过不使用 T4MVC 并在操作链接参数中指定空值,如下所示,但仍然得到 错误的 url

@Html.ActionLink("Back to Doctors", "Index", "Doctors", null, null)

我只是在 adpan 区域工作,有 2 条路线。

如果可能的话,我将不胜感激从那个错误的网址获得正确视图的原因。

【问题讨论】:

    标签: asp.net-mvc url routes asp.net-mvc-areas t4mvc


    【解决方案1】:

    路由值不仅来自ActionLink 的参数,还来自bleed over from the current request。虽然很多人不觉得这很直观,但它让stay within the current culture 或网站区域等操作变得非常容易。

    元数据的路由值不是。它们是在匹配 URL 模式或确定使用哪个路由来构建传出 URL 时将使用的值。我怀疑这就是您正在努力解决的问题,因为将路由值设置为 URL 是非常不寻常的。

    如果您需要将元数据与路由信息一起传递,可以使用 DataTokens 属性来实现此目的。虽然两者都通过请求传递,但仅使用路由值来确定路由是否匹配。

    也可以通过显式指定来覆盖当前请求中的路由值。

    @Html.RouteLink("foobar", new { controller = "Home", action = "Index", doctorObj = "" })
    

    但由于您需要在每个受影响的链接上执行此操作,因此首先将 invalid 数据排除在路由值之外更为实用。

    【讨论】:

    • 留在当前文化和地区的充分理由。但是你能告诉我你所说的元数据到底是什么意思吗?
    • 元数据是指实际上并不控制 URL 构建方式的数据,而是用于其他应用程序目的的数据。例如,MvcCodeRouting 使用DataTokens 根据其命名空间确定当前控制器距站点根目录的深度,这决定了将多少段放入 URL。但该信息不用于将路由与传入请求进行匹配。
    • 我感到困惑。我尝试做的是在 adpan_clinics 路由中生成第一个 url。我还想避免像 ?doctorObj=doctor1 这样的 url 参数,因此,我使用模型绑定来获得整洁的 url。您建议的解决方案给了我这个网址:localhost/adpan/Doctors?doctorObj=doctor1。省略医生Obj="" 再次给我错误的网址。我无法理解它
    【解决方案2】:

    基于@NightOwl888 answerdifference between RouteLink and ActionLink,我找到了硬编码方法和T4MVC 方法的解决方案。

    1)硬编码方法:必须指定路线名称:

    //signiture: RouteLink(this HtmlHelper htmlHelper, string linkText, string routeName, object routeValues);
    @Html.RouteLink("Back to Doctors","adpan_default", new { controller = "Doctors", action = "Index"})
    

    结果网址:http://localhost/adpan/Doctors

    2) T4MVC 方法:必须指定路由名称:

    //signiture: RouteLink(this HtmlHelper htmlHelper, string linkText, string routeName, ActionResult result, IDictionary<string, object> htmlAttributes)
    @Html.RouteLink("Back to Doctors","adpan_default", MVC.adpan.Doctors.Index().AddRouteValues(new {Area=""}), null)
    

    结果网址:http://localhost/adpan/Doctors

    为什么要AddRouteValues(new {Area=""})

    由于here 的讨论,这似乎是 T4MVC 中的一个错误。下面的路由链接将 ?Area=adpan 作为无效参数添加到 url:

    @Html.RouteLink("Back to Doctors", "adpan_default", MVC.adpan.Doctors.Index(), null)
    

    结果网址:http://localhost/adpan/Doctors?Area=adpan

    但是,这可能是一种欺骗 T4MVC 中不需要的 url 参数的技巧。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-09
      • 2017-05-14
      • 2022-01-21
      • 2016-09-01
      • 2018-03-08
      • 1970-01-01
      • 1970-01-01
      • 2021-08-05
      相关资源
      最近更新 更多