@ Url.RouteUrl在ASP.Net MVC 5中为空

时间:2018-01-23 17:19:43

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

我在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 }
        );
    }
}

1 个答案:

答案 0 :(得分:0)

来自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>
相关问题