【问题标题】:How to change URL in the client with a redirect如何使用重定向更改客户端中的 URL
【发布时间】:2019-10-24 10:07:42
【问题描述】:

我正在使用 C# core 2.2 MVC 并尝试实现友好的 URL。 我已经开始实施重写规则,这工作正常。

但是

浏览器中的 URL 从友好的 URL 变为丑陋的控制器/动作/{id}。

public void ApplyRule(RewriteContext context)
{
    var request = context.HttpContext.Request;
    var path = request.Path.ToString().ToLower();

    if (_friendlyUrlMap.ContainsKey(path))
    {
        var redirectUrl = _friendlyUrlMap[path];

        var response = context.HttpContext.Response;

        // Attempt - 01
        response.StatusCode = StatusCodes.Status301MovedPermanently;
        context.Result = RuleResult.EndResponse;
        response.Headers[HeaderNames.Location] = redirectUrl;
    }
}

我想要的是我做错了什么,并将 URL 保留为友好 URL。

【问题讨论】:

    标签: c# asp.net-core .net-core


    【解决方案1】:

    我想要的是我做错了什么,并将 URL 保留为友好 URL。

    你可以直接在你的ApplyRule方法中更改redirectUrl的路径,我给_friendlyUrlMap设置了一些值方便测试,因为我不知道_friendlyUrlMap的定义:

     public void ApplyRule(RewriteContext context)
        {
            var request = context.HttpContext.Request;
            var path = request.Path.ToString().ToLower();
    
            var _friendlyUrlMap = new Dictionary<string, string>();
            _friendlyUrlMap.Add("/filetotexttohtml", "/Home/FileToTextToHtml");
            _friendlyUrlMap.Add("/convertfile", "/Home/FileToTextToHtml");
    
            if (_friendlyUrlMap.ContainsKey(path))
            {
                var redirectUrl = _friendlyUrlMap[path];
                var response = context.HttpContext.Response;
                context.HttpContext.Request.Path = redirectUrl;
            }
        }
    

    您可以在 Startup.cs 中添加 ApplyRule 方法,然后将其添加到 Configure 方法中,如下所示:

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }
    
            app.UseHttpsRedirection();
            app.UseStaticFiles();
            app.UseCookiePolicy();
    
            app.UseRewriter(new RewriteOptions()
                           .Add(ApplyRule)
                           );
    
            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });
        }
    

    结果:

    【讨论】:

    • 抱歉,我收到了 404。你在 startup.cs public void Configure(IApplicationBuilder app, IHostingEnvironment env) 的顺序中添加了这个?
    • @StephenCossgrove ,请检查我在 startup.cs 的配置方法中的更新
    猜你喜欢
    • 2012-02-14
    • 2015-07-15
    • 2021-04-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-24
    • 1970-01-01
    • 1970-01-01
    • 2013-01-30
    相关资源
    最近更新 更多