简体   繁体   English

是否可以在 .net core 中重定向来自中间件的请求

[英]Is it possible to redirect request from middleware in .net core

What I'm trying to achieve is this: When someone visits: smartphone.webshop.nl/home/index I want to redirect this from middle ware to: webshop.nl/smartphone/home/index我想要实现的是:当有人访问: smartphone.webshop.nl/home/index我想将它从中间件重定向到: webshop.nl/smartphone/home/index

I want to do this because I want to create a generic controller which get data from database based on sub-domein .我想这样做是因为我想创建一个通用控制器,它基于sub-domein从数据库中获取数据。 So I need all the calls come to the same controller.所以我需要所有的调用都来自同一个控制器。

This is my middleware now:这是我现在的中间件:

public Task Invoke(HttpContext context)
    {
        var subDomain = string.Empty;

        var host = context.Request.Host.Host;

        if (!string.IsNullOrWhiteSpace(host))
        {
            subDomain = host.Split('.')[0]; // Redirect to this subdomain
        }

        return this._next(context);
    }

How can I redirect and how should my controller/mvc config look like?我如何重定向以及我的controller/mvc配置应该如何?

I'm pretty new to .net core so please be clear in your answers.我对 .net core 还很陌生,所以请在你的答案中说清楚。 Thank you.谢谢你。

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Antiforgery;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity;

namespace Test.Middleware
{
    public class TestMiddleware
    {
        private readonly RequestDelegate _next;
        public TestMiddleware(RequestDelegate next)
        {
            _next = next;
        }
        public async Task InvokeAsync(HttpContext httpContext, AppDbContext dataContext, UserManager<User> userManager, IAntiforgery antiforgery)
        {

            // Redirect to login if user is not authenticated. This instruction is neccessary for JS async calls, otherwise everycall will return unauthorized without explaining why
            if (!httpContext.User.Identity.IsAuthenticated && httpContext.Request.Path.Value != "/Account/Login")
            {
                httpContext.Response.Redirect("/Account/Login");
            }

            // Move forward into the pipeline
            await _next(httpContext);
        }
    }
    public static class TestMiddlewareExtensions
    {
        public static IApplicationBuilder UseTestMiddleware(this IApplicationBuilder builder)
        {
            return builder.UseMiddleware<TestMiddleware>();
        }
    }
}

That's called URL Rewriting and ASP.NET Core already have special middleware for that (in package Microsoft.AspNetCore.Rewrite )这就是所谓的 URL 重写,ASP.NET Core 已经为此提供了特殊的中间件(在Microsoft.AspNetCore.Rewrite包中)

Check docs, may be you may use it "as is".检查文档,也许您可​​以“按原样”使用它。

If not - you can check source code and write your own.如果没有 - 您可以检查源代码并编写自己的代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是否可以从 .NET Core 中间件中检索控制器的操作结果? - Is it possible to retrieve controller's action result from .NET Core middleware? .Net核心中间件-从请求中获取表单数据 - .Net Core Middleware - Getting Form Data from Request 从.net Core 2.1中间件重定向到控制器操作/视图的正确方法 - Proper Way to Redirect to controller action/view from .net core 2.1 middleware Asp.Net中的重定向核心中间件不呈现目标页面 - Redirect in Asp.Net Core middleware does not render target page 从 ASP.NET Core 2.0 中的中间件获取 HttpContext 中的请求正文 - getting the request body inside HttpContext from a Middleware in asp.net core 2.0 如何从ASP.NET Core 2.0中的自定义中间件请求身份验证 - How to request authentication from custom middleware in ASP.NET Core 2.0 从中间件中排除路由 - .net core - Exclude route from middleware - .net core 是否可以在 Middleware Asp .Net Core 中调用 WebApi - Is it possible to call WebApi inside Middleware Asp .Net Core 在 .Net Core 3.0 中间件中使用自定义标头填充 HTTP 请求标头 - Populate HTTP request headers with custom header in .Net Core 3.0 middleware 中间件异常后获取请求正文。 .NET 核心 3.1 - Get request body after exception in middleware. .NET Core 3.1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM