【问题标题】:How can i rewrite urls with .json to .ashx using ASP.NET handlers如何使用 ASP.NET 处理程序将带有 .json 的 url 重写为 .ashx
【发布时间】:2020-01-30 18:14:04
【问题描述】:

我已经更改了我的 ASP.Net 应用程序的体系结构,我有很多引用 .json 文件的链接,这些文件现在由 .ashx 文件提供。 为了保持向后兼容性并避免更改现有链接,我尝试使用 ASP.Net 处理程序来重命名传入的 http 请求。

我尝试了许多在 Stackoverflow 上找到的解决方案,但我无法让它发挥作用:

public bool IsReusable
{
    get { return false; }
}

public void ProcessRequest(HttpContext context)
{
    context.Response.ContentType = "text/json";

    string newUrl = context.Request.RawUrl.Replace(".json", ".ashx");
    context.Server.Transfer(newUrl);

}

【问题讨论】:

  • 您能否添加更多详细信息,例如您的 Startup.cs 以及转换或重新路由背后的实际意图?
  • ashx 是 HTTP 处理程序的扩展。它们不是要链接到的页面,它们更接近于今天的 Controller 操作甚至 MapEndpoint 所做的事情。那是你想做的吗?在 ASP.NET Core 中模拟 HTTP 处理程序?

标签: c# asp.net-core


【解决方案1】:

看看URL Rewriting Middleware in ASP.NET CoreASP.NET Core URL Rewriting

你可以使用 URL Rewriting 中间件来重写类似这个startup.cs的url:

using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Rewrite;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Net.Http.Headers;

namespace WebApplication1
{
    public class Startup
    {
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            var options = new RewriteOptions()
                .Add(RedirectJsonFileRequests);
            app.UseRewriter(options);

            app.Run(async (context) =>
            {
                await context.Response.WriteAsync("Hello World!");
            });
        }

        /// <summary>
        /// Redirect all requests to .json extension files to .ashx extensions, copy across the query string, if any
        /// </summary>
        /// <param name="context"></param>
        public static void RedirectJsonFileRequests(RewriteContext context)
        {
            var request = context.HttpContext.Request;

            // Because the client is redirecting back to the same app, stop 
            // processing if the request has already been redirected.
            if (request.Path.Value.EndsWith(".ashx", StringComparison.OrdinalIgnoreCase))
            {
                return;
            }

            if (request.Path.Value.EndsWith(".json", StringComparison.OrdinalIgnoreCase))
            {
                var response = context.HttpContext.Response;
                response.StatusCode = StatusCodes.Status301MovedPermanently;
                context.Result = RuleResult.EndResponse;
                response.Headers[HeaderNames.Location] =
                    request.Path.Value.Replace(".json", ".ashx", StringComparison.OrdinalIgnoreCase) + request.QueryString;
            }
        }
    }
}

【讨论】:

    猜你喜欢
    • 2011-11-09
    • 2010-10-12
    • 2013-03-25
    • 2011-11-16
    • 2011-03-22
    • 2013-11-17
    • 2010-10-11
    • 2011-10-14
    • 1970-01-01
    相关资源
    最近更新 更多