本文介紹了如何使 IRouteConstraint 過濾路由的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!
問題描述
限時送ChatGPT賬號..
我編寫了一個自定義路由約束,但它的過濾器無法識別.有人有使用 IRouteConstraint 的示例嗎?
I wrote a custom route constraint, but its filter just doesn't get recognized. Does anyone have an example working use of IRouteConstraint ?
另外,請注意開發(fā)人員:我在我的 android 上獲得了雙重顯示的表單.部分渲染一定有問題?
Also, note to developers: I get double display of the form on my android. Something must be wrong with the partial rendering?
推薦答案
這是一個在虛構(gòu)存儲庫中查找文章 slug 的簡單約束:
Here's a simple constraint that looks up an article slug in a fictional repository:
public class SlugRouteConstraint : IRouteConstraint
{
private readonly ISlugRepository slugRepository = new SlugRepository();
public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
{
if (!values.ContainsKey(parameterName))
return false;
var slug = (string)values[parameterName];
return slugRepository.Exists(slug);
}
}
你可以像這樣連接約束:
You could wire up the constraint like this:
routes.MapRoute("Slugs", "{slug}",
new { controller = "Articles", action = "View" },
new { slug = new SlugConstraint() });
這篇關(guān)于如何使 IRouteConstraint 過濾路由的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!