有没有办法强制我的 ASP.NET Core 2.0 应用程序使用我在各处编写的自定义 UrlHelper?
我有一个具有自定义逻辑的类
public class CustomUrlHelper : UrlHelper { ... }
我希望它能在任何地方使用,以便根据我们自定义的业务规则生成 url。
创建一个CustomUrlHelper
and a CustomUrlHelperFactory
.
public class CustomUrlHelper : UrlHelper
{
public CustomUrlHelper(ActionContext actionContext)
: base(actionContext) { }
public override string Action(UrlActionContext actionContext)
{
var controller = actionContext.Controller;
var action = actionContext.Action;
return $"You wrote {controller} > {action}.";
}
}
public class CustomUrlHelperFactory : IUrlHelperFactory
{
public IUrlHelper GetUrlHelper(ActionContext context)
{
return new CustomUrlHelper(context);
}
}
然后打开你的Statup.cs
归档并登记CustomUrlHelperFactory
在里面ConfigureServices
method.
services.AddMvc();
services.AddSingleton<IUrlHelperFactory, CustomUrlHelperFactory>();
完成此操作后,您的应用程序将使用CustomUrlHelper
到处。其中包括致电@Url.Action("Index", "Home")
从剃刀页面。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)