ape*_*ero 6 .net c# asp.net-mvc asp.net-core
我要实现的目标是:当有人访问时:smartphone.webshop.nl/home/index
我想将其从中间件重定向到:webshop.nl/smartphone/home/index
我要这样做是因为我想创建一个通用控制器,该控制器从上从数据库获取数据sub-domein。因此,我需要所有呼叫都到达同一控制器。
现在这是我的中间件:
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);
}
Run Code Online (Sandbox Code Playgroud)
我如何重定向以及我的controller/mvc配置应如何显示?
我对.net core来说还很陌生,因此请在回答中明确说明。谢谢。
Iva*_*rta 13
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>();
}
}
}
Run Code Online (Sandbox Code Playgroud)