本文介绍了与{controller} / {action}不同的ASP.NET MVC映射URL与区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够使用不符合{controller} / {action} / {id}格式的URL映射路线。映射如下:

I want to be able to map a route using a URL that doesn't conform to the {controller}/{action}/{id} format. The mapping looks like:

routes.CreateArea("Root", "MyApp.Web.Controllers",
    routes.MapRoute("Category-List", "Category/{category}",
        new { controller = "Category", action = "List" }),
    routes.MapRoute("Default", "{controller}/{action}/{id}",
        new { controller = "Home", action = "Index", id = "" })
);

在我有 CategoryController 的地方列表(字符串类别)。

Where I have a CategoryController with an action List(string category).

我希望能够在我的观点中使用它:

I was hoping to be able to use this in my view:

<%= Html.ActionLink<CategoryController>(
    c => c.List(category.UrlFriendlyName),
    category.Name)%>

(添加了换行符以提高可读性)

(line breaks added for readability)

产生的所有内容都是带有 href = 的链接。从根区域删除路由会产生正确的结果。是否可以将这种类型的映射与通用ActionLink帮助器一起使用,还是必须诉诸RouteLink或类似的具有硬编码值的东西?

All this produces is a link with href="". Removing the route from the "Root" area produces the correct result. Is it possible to use this type of mapping with the generic ActionLink helper or do I have to resort to RouteLink or something similar with hard coded values?

我还尝试了以下方法没有成功:

I also tried the following with no success:

<%= Html.ActionLink(category.Name, "List", "Category",
new { category = category.UrlFriendlyName }) %>


推荐答案

不理想,但是可以使用路由名称方法吗?

Not ideal, but can you use the route name approach?

<%= Html.RouteLink("your link", "Category-List", new {category = "foo"})%>

这篇关于与{controller} / {action}不同的ASP.NET MVC映射URL与区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 09:13