我在C#中使用ASP.NET MVC 4。我使用的是区域,它的名字与" Admin"
相同这是我的路线配置;
public static class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(name: "PageBySlug",
url: "{slug}",
defaults: new {controller = "Home", action = "RenderPage"},
constraints: new {slug = ".+"});
routes.MapRoute(name: "Default",
url: "{controller}/{action}/{id}",
defaults: new {controller = "Home", action = "Index", id = UrlParameter.Optional},
namespaces: new[] { "Web.Frontend.Controllers.Controllers" });
}
}
我生成了前端页面链接; "产品/苹果iphone" 所以我想这样称呼他们。
但错误是:代码无法获得控制器/操作方法。 我使用了前端页面链接;
@Html.ActionLink(linkItem.Title, "RenderPage", routeValues: new {controller = "Home", slug = linkItem.PageSlug})
@Html.RouteLink(linkItem.Title, routeName: "PageBySlug", routeValues: new { controller = "Home", action = "RenderPage", slug = linkItem.PageSlug })
<a href="@Url.Action("RenderPage", "Home", new {slug = linkItem.PageSlug})">@linkItem.Title</a>
<a href="@Url.RouteUrl("PageBySlug", new { controller = "Home", action = "RenderPage", slug = linkItem.PageSlug})">@linkItem.Title</a>
他们正在渲染网址链接; http://localhost:1231/products/apple-iphone 这就像我想要的那样。但是当我点击任何链接时,asp.net mvc给了我这个错误:
&#39; /&#39;中的服务器错误应用
无法找到资源。
描述:HTTP 404.您正在查找的资源(或其中一个依赖项)可能已被删除,名称已更改或暂时不可用。请查看以下网址,确保拼写正确。
请求的网址:/ products / apple-iphone
版本信息:Microsoft .NET Framework版本:4.0.30319; ASP.NET版本:4.6.1069.1
这是我的控制器;
namespace Web.Frontend.Controllers
{
public class HomeController : BaseFrontendController
{
public ActionResult Index()
{
return View();
}
public ActionResult RenderPage(string slug)
{
return View();
}
}
}
那么如何才能捕获像这个组合slug的每个链接请求并转换我的编码视图?
答案 0 :(得分:1)
问题是,当您请求products/iphone
时,路由引擎不知道您是说“产品/ iphone”或控制器“产品”和操作方法“iphone”。
您可以编写自定义路由约束来处理此问题。此约束将检查url的slug部分是否为有效控制器,如果是,则执行控制器操作。
public class SlugConstraint : IRouteConstraint
{
public bool Match(HttpContextBase httpContext, Route route, string parameterName,
RouteValueDictionary values, RouteDirection routeDirection)
{
var asm = Assembly.GetExecutingAssembly();
//Get all the controller names
var controllerTypes = (from t in asm.GetExportedTypes()
where typeof(IController).IsAssignableFrom(t)
select t.Name.Replace("Controller", ""));
var slug = values["slug"];
if (slug != null)
{
if (controllerTypes.Any(x => x.Equals(slug.ToString(),
StringComparison.OrdinalIgnoreCase)))
{
return false;
}
else
{
var c = slug.ToString().Split('/');
if (c.Any())
{
var firstPart = c[0];
if (controllerTypes.Any(x => x.Equals(firstPart,
StringComparison.OrdinalIgnoreCase)))
{
return false;
}
}
}
return true;
}
return false;
}
}
现在,在为slug注册自定义路径定义时使用此路径约束。确保在路由模式中使用{*slug}
。 *
表示它是任何东西(例如:“a / b / c”)(可变数量的网址片段 - 更像是一个全部捕获)
routes.MapRoute(name: "PageBySlug",
url: "{*slug}",
defaults: new { controller = "Home", action = "RenderPage" },
constraints: new { slug = new SlugConstraint() }
, namespaces: new string[] { "Web.Frontend.Controllers.Controllers" });
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
, new string[] { "Web.Frontend.Controllers.Controllers" });
答案 1 :(得分:1)
您只能提供此类链接
<a href="@Url.RouteUrl("PageBySlug", new {slug = linkItem.PageSlug})">@linkItem.Title</a>
因为路线表使用您提供的路线名称找到您的路线。因此不需要控制器名称和操作名称。