如何将asp.net-core应用根路由至swagger?
- 内容介绍
- 文章标签
- 相关推荐
本文共计250个文字,预计阅读时间需要1分钟。
要将请求从www.mysite.com重定向到www.mysite.com/swagger,你可以使用HTTP重定向功能。以下是在Startup.cs中配置HTTP重定向的简单方法:
csharppublic void Configure(IApplicationBuilder app, IWebHostEnvironment env){ if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); }
app.UseRouting();
app.UseEndpoints(endpoints=> { endpoints.MapControllerRoute( name: default, pattern: {controller=Home}/{action=Index}/{id?});
// 添加重定向规则 endpoints.MapGet(swagger, context=> { context.Response.Redirect(/swagger); context.Response.StatusCode=StatusCodes.Status302Found; }); });}
这段代码中,我们使用`MapGet`方法添加了一个路由,当请求到`swagger`时,它会重定向到`/swagger`,并且返回HTTP状态码302Found。
将请求从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);
本文共计250个文字,预计阅读时间需要1分钟。
要将请求从www.mysite.com重定向到www.mysite.com/swagger,你可以使用HTTP重定向功能。以下是在Startup.cs中配置HTTP重定向的简单方法:
csharppublic void Configure(IApplicationBuilder app, IWebHostEnvironment env){ if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); }
app.UseRouting();
app.UseEndpoints(endpoints=> { endpoints.MapControllerRoute( name: default, pattern: {controller=Home}/{action=Index}/{id?});
// 添加重定向规则 endpoints.MapGet(swagger, context=> { context.Response.Redirect(/swagger); context.Response.StatusCode=StatusCodes.Status302Found; }); });}
这段代码中,我们使用`MapGet`方法添加了一个路由,当请求到`swagger`时,它会重定向到`/swagger`,并且返回HTTP状态码302Found。
将请求从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);

