C# 是否可以重定向.NETCore中中间件的请求
我试图实现的是: 当有人访问时:C# 是否可以重定向.NETCore中中间件的请求,c#,.net,asp.net-mvc,asp.net-core,C#,.net,Asp.net Mvc,Asp.net Core,我试图实现的是: 当有人访问时:smartphone.webshop.nl/home/index 我想将它从中间件重定向到:webshop.nl/smartphone/home/index 我想这样做是因为我想创建一个通用控制器,它根据子域从数据库中获取数据。所以我需要所有的电话都到同一个控制器 这是我现在的中间件: public Task Invoke(HttpContext context) { var subDomain = string.Empty;
smartphone.webshop.nl/home/index
我想将它从中间件重定向到:webshop.nl/smartphone/home/index
我想这样做是因为我想创建一个通用控制器,它根据子域
从数据库中获取数据。所以我需要所有的电话都到同一个控制器
这是我现在的中间件:
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);
}
如何重定向以及我的控制器/mvc
配置应该是什么样子
我对.NETCore还很陌生,所以请在回答中明确。谢谢。这就是所谓的URL重写,ASP.NET Core已经为此提供了支持(在包
Microsoft.AspNetCore.Rewrite
中)
检查文档,可能是您可以“按原样”使用它
如果没有,您可以自己编写。使用系统;
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>();
}
}
}
使用System.Collections.Generic;
使用System.IO;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用Microsoft.AspNetCore.Antiforgery;
使用Microsoft.AspNetCore.Builder;
使用Microsoft.AspNetCore.Http;
使用Microsoft.AspNetCore.Identity;
命名空间测试中间件
{
公共类测试中间件
{
private readonly RequestDelegate\u next;
公共测试中间件(RequestDelegate next)
{
_下一个=下一个;
}
公共异步任务InvokeAsync(HttpContext HttpContext、AppDbContext dataContext、UserManager UserManager、IAntiforgery antiforgery)
{
//如果用户未经身份验证,则重定向到登录。此指令对于JS异步调用是必需的,否则everycall将返回unauthorized而不解释原因
如果(!httpContext.User.Identity.IsAuthenticated&&httpContext.Request.Path.Value!=“/Account/Login”)
{
httpContext.Response.Redirect(“/Account/Login”);
}
//进入管道
等待下一步(httpContext);
}
}
公共静态类TestMiddlewareExtensions
{
公共静态IAApplicationBuilder UseTestMiddleware(此IAApplicationBuilder)
{
返回builder.UseMiddleware();
}
}
}
你看过上下文.响应.重定向吗?@ColinM是的。返回302并重写url。但我不想那样。我希望url保持原样,并且只在代码中重定向它。非常感谢@DmitrySource code link is dead@Dmitry。你能更新url吗?为什么不在这里举个例子说明如何更新?如果你需要更多的控制何时按照我的要求重定向,而不是固定不变的重定向,那么这是一个很好的解决方案!谢谢