如何将slug添加到asp.net核心网站中的所有Link生成?

问题描述 投票:6回答:3

我需要能够控制我的Url.Content("~")调用生成的链接,以便能够在链接的开头接受Slug。基本上托管URL将位于负载均衡器后面,可能位于根级别或更友好的URL后面...

举个例子:该网站配置为在http://localhost:5001下运行,因此Url.Content("~/scripts/site.js")将生成"/scripts/site.js"

如果浏览器直接访问该URL或甚至是别名(如www.mysite.com),这都没问题。

但我希望o能够灵活地在www.mysite.com/Slug下托管该网站(想想证书等)......

现在我生成的链接转到www.mysite.com/scripts.site.js,它解析为404。

理想情况下,slug可以配置为自定义IUrlHelper,甚至是自定义LinkGenerator,但我似乎无法注入那些并覆盖当前的那些。

我试过了:

services.AddScoped<IUrlHelper>(x =>
            {
                var actionContext = x.GetService<IActionContextAccessor>().ActionContext;
                return new MyCustomUrlHelper(actionContext);
            });

但无法注入。当我尝试调试时,我注意到如果你在控制器中调用相同的命令,你会得到一个Microsoft.AspNetCore.Mvc.Routing.EndpointRoutingUrlHelper的实例。

有没有办法在不创建自定义帮助程序的情况下更改它(因为在某些区域会丢失并且几乎不可能找到被滥用的帮助程序)

c# asp.net-core-mvc slug urlhelper
3个回答
2
投票

直接绑定IUrlHelper无效,因为MVC在内部使用工厂解析实例。要在控制器和剃刀视图中获取自己的自定义URL帮助程序的实例,您需要在启动类中提供IUrlHelperFactory的自定义实现。

以下代码段允许您使用自己的功能装饰原始URL帮助程序:

在你的Startup类中,你需要在IUrlHelperFactory之后为AddMvc添加单一范围的自定义实现:

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
    services.AddSingleton<IUrlHelperFactory, CustomUrlHelperFactory>();
}

自定义实现可能如下所示:

public class CustomUrlHelper : IUrlHelper
{
    private IUrlHelper _originalUrlHelper;

    public ActionContext ActionContext { get; private set; }

    public CustomUrlHelper(ActionContext actionContext, IUrlHelper originalUrlHelper)
    {
        this.ActionContext = actionContext;
        this._originalUrlHelper = originalUrlHelper;
    }

    public string Action(UrlActionContext urlActionContext)
    {
        return _originalUrlHelper.Action(urlActionContext);
    }

    public string Content(string contentPath)
    {
        return _originalUrlHelper.Content(contentPath);
    }

    public bool IsLocalUrl(string url)
    {
        return _originalUrlHelper.IsLocalUrl(url);
    }

    public string Link(string routeName, object values)
    {
        return _originalUrlHelper.Link(routeName, values);
    }

    public string RouteUrl(UrlRouteContext routeContext)
    {
        return _originalUrlHelper.RouteUrl(routeContext);
    }
}

public class CustomUrlHelperFactory : IUrlHelperFactory
{
    public IUrlHelper GetUrlHelper(ActionContext context)
    {
        var originalUrlHelperFactory = new UrlHelperFactory();
        var originalUrlHelper = originalUrlHelperFactory.GetUrlHelper(context);
        return new CustomUrlHelper(context, originalUrlHelper);
    }
}

1
投票

IUrlHelper默认不可注入。

您将不得不修改您的startup.cs代码作为explained in this blog.

您必须先注册IActionContextAccessor。

然后在UrlHelperFactory的帮助下,您可以注入自定义实现,如下所示:

services.AddSingleton<IActionContextAccessor, ActionContextAccessor>();
services.AddScoped<IUrlHelper>(x => {
    var actionContext = x.GetRequiredService<IActionContextAccessor>().ActionContext;
    var factory = x.GetRequiredService<IUrlHelperFactory>();
    return factory.GetUrlHelper(actionContext);
});

IActionContextAccessorIUrlHelperFactory都生活在Microsoft.AspNetCore.Mvc.Core包中。

如果你正在使用Microsoft.AspNetCore.All元数据包,你应该已经引用了这个。

这应该可以帮助您解决问题。


-1
投票

您是否可以通过字符串连接强制一些灵活性进入您的解决方案,如下所示:

public string SlugUrl(string slug, string url, bool tilde = false)
{
    if (tilde) then
    {
        return Url.Content("~" + slug + url);
    }
    else
    {
        return Url.Content(slug + url);
    }
}

[...]

string slug1 = "www.mysite.com";
string slug2 = "www.mysite.com/Slug";
string trailUrl = "/scripts/site.js";
string result1 = SomeClass.SlugUrl(slug1, trailUrl);
string result2 = SomeClass.SlugUrl(slug2, trailUrl);
string result3 = SomeClass.SlugUrl(slug1, trailUrl, true);
string result4 = SomeClass.SlugUrl(slug2, trailUrl, true);

等等...

© www.soinside.com 2019 - 2024. All rights reserved.