如何将asp.net-core应用根路由至swagger?
- 内容介绍
- 文章标签
- 相关推荐
本文共计444个文字,预计阅读时间需要2分钟。
要将请求从www.mysite.com重定向到www.mysite.com/swagger,可以通过以下几种方法实现:
1. 修改Web服务器配置: - 如果使用的是IIS,可以在网站或虚拟目录的HTTP重定向设置中进行配置。 - 如果使用的是Apache,可以在`.htaccess`文件中添加重定向规则。
2. 在Startup.cs中配置中间件: csharp public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); }
app.UseRouting();
app.UseEndpoints(endpoints=> { endpoints.MapControllers(); });
// 重定向逻辑 app.Use(async (context, next)=> { if (context.Request.Path.StartsWithSegments(/swagger)) { context.Response.Redirect(/swagger); return; } await next(); }); }
3. 使用路由配置: 在`Startup.cs`的`Configure`方法中,可以使用路由来处理重定向。 csharp app.UseRouting();
app.UseEndpoints(endpoints=> { endpoints.MapControllerRoute( name: default, pattern: {controller=Home}/{action=Index}/{id?});
endpoints.MapControllerRoute( name: swagger, pattern: swagger/{*pathInfo}, defaults: new { controller=Swagger, action=Index }); });
4. 使用全局过滤器: 创建一个全局过滤器,检查请求路径,如果匹配则进行重定向。 csharp public class RedirectFilter : IActionFilter { public void OnActionExecuting(ActionExecutingContext context) { if (context.ActionDescriptor.ActionName==Index && context.ActionDescriptor.ControllerName==Swagger) { context.Result=new RedirectResult(/swagger); } }
public void OnActionExecuted(ActionExecutedContext context) { } }
然后在Startup.cs中注册过滤器: csharp services.AddControllers().AddFilter(new RedirectFilter());
以上方法都可以实现从www.mysite.com重定向到www.mysite.com/swagger。根据你的具体需求和环境选择合适的方法。
将请求从www.mysite.com重定向到www.mysite.com/swagger的正确方法是什么?我设置了一个索引控制器装饰路线(“”),它的工作原理,但看起来像一个Kludge.我假设应该有一种方法在Startup.cs中的MVC路由中指定它,我只是想不出来.
// kludge code, how do I do this in app.UseMvc(route => route.MapRoute... [ApiExplorerSettings(IgnoreApi = true)] [Route("")] public class IndexController : Controller { public ActionResult Index() { return Redirect("/swagger"); } } 请尝试以下重定向规则:
var option = new RewriteOptions(); option.AddRedirect("^$", "swagger"); app.UseRewriter(option);
本文共计444个文字,预计阅读时间需要2分钟。
要将请求从www.mysite.com重定向到www.mysite.com/swagger,可以通过以下几种方法实现:
1. 修改Web服务器配置: - 如果使用的是IIS,可以在网站或虚拟目录的HTTP重定向设置中进行配置。 - 如果使用的是Apache,可以在`.htaccess`文件中添加重定向规则。
2. 在Startup.cs中配置中间件: csharp public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); }
app.UseRouting();
app.UseEndpoints(endpoints=> { endpoints.MapControllers(); });
// 重定向逻辑 app.Use(async (context, next)=> { if (context.Request.Path.StartsWithSegments(/swagger)) { context.Response.Redirect(/swagger); return; } await next(); }); }
3. 使用路由配置: 在`Startup.cs`的`Configure`方法中,可以使用路由来处理重定向。 csharp app.UseRouting();
app.UseEndpoints(endpoints=> { endpoints.MapControllerRoute( name: default, pattern: {controller=Home}/{action=Index}/{id?});
endpoints.MapControllerRoute( name: swagger, pattern: swagger/{*pathInfo}, defaults: new { controller=Swagger, action=Index }); });
4. 使用全局过滤器: 创建一个全局过滤器,检查请求路径,如果匹配则进行重定向。 csharp public class RedirectFilter : IActionFilter { public void OnActionExecuting(ActionExecutingContext context) { if (context.ActionDescriptor.ActionName==Index && context.ActionDescriptor.ControllerName==Swagger) { context.Result=new RedirectResult(/swagger); } }
public void OnActionExecuted(ActionExecutedContext context) { } }
然后在Startup.cs中注册过滤器: csharp services.AddControllers().AddFilter(new RedirectFilter());
以上方法都可以实现从www.mysite.com重定向到www.mysite.com/swagger。根据你的具体需求和环境选择合适的方法。
将请求从www.mysite.com重定向到www.mysite.com/swagger的正确方法是什么?我设置了一个索引控制器装饰路线(“”),它的工作原理,但看起来像一个Kludge.我假设应该有一种方法在Startup.cs中的MVC路由中指定它,我只是想不出来.
// kludge code, how do I do this in app.UseMvc(route => route.MapRoute... [ApiExplorerSettings(IgnoreApi = true)] [Route("")] public class IndexController : Controller { public ActionResult Index() { return Redirect("/swagger"); } } 请尝试以下重定向规则:
var option = new RewriteOptions(); option.AddRedirect("^$", "swagger"); app.UseRewriter(option);

