【问题标题】:@Url.RouteUrl is empty in ASP.Net MVC 5@Url.RouteUrl 在 ASP.Net MVC 5 中为空
【发布时间】:2018-07-02 14:38:46
【问题描述】:

我在 ASP.Net MVC 5 中的控制器上有以下操作:

[Route("Sprache/{languageName}/Kurs/{courseName}/Text/{value}/Test/{referenzID}", Name = "Textauswahl")]        
public ActionResult SelectedText(string languageName, string courseName, string value, string referenzID)
{

    return View("ShowTextView");
}

但是,当我尝试通过以下链接生成链接时,它始终为空。

<a href="@Url.RouteUrl("Textauswahl", new { Model.Training.LanguageName, Model.Training.CourseName, Model.Training.Category ,item })">Auswählen</a>

Screenshot from Browser

这是我的 RouteConfig:

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

        routes.MapMvcAttributeRoutes();

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

【问题讨论】:

    标签: c# asp.net asp.net-mvc asp.net-mvc-4 razor


    【解决方案1】:

    来自Url.RouteUrl 的空响应表示没有路由匹配您提供的参数。传递路由名称只会将匹配条件缩小到特定路由,但所有路由值仍必须存在(显式传递或在当前 HTTP 请求中)才能生成 URL。

    请注意,您使用的语法不正确。传递给RouteUrl 的值必须同时具有键和值,或者传递的变量的名称必须与路由中使用的路由键匹配。

    <a href="@Url.RouteUrl("Textauswahl", new { 
       languageName = Model.Training.LanguageName, 
       courseName = Model.Training.CourseName, 
       value = Model.Training.Category,
       referenzID = item })">Auswählen</a>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-21
      • 2017-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-24
      • 2014-11-18
      相关资源
      最近更新 更多