[英].net core web api controller and action selection
I need to generate appropriate 404 erors according to:我需要根据以下内容生成适当的 404 错误:
I'm trying to migrate web api on .NET Core Web API and need to find analog in .net core of DefaultHttpControllerSelector and ApiControllerActionSelector , which was in .net framework.我正在尝试在 .NET Core Web API 上迁移 web api,并且需要在 DefaultHttpControllerSelector 和 ApiControllerActionSelector 的 .net 核心中找到模拟,这是在 .net 框架中。 Because I have modules, which checks controller and action existance.
因为我有模块,它检查控制器和动作的存在。 And generate custom error if controller or action haven't found.
如果没有找到控制器或动作,则生成自定义错误。 How I can handle this?
我该如何处理?
For controller or action not exist, it will return 404 by default.对于不存在的控制器或动作,默认返回404 。
If you want to custom the Error Message, you could try Middleware
to check the Response.StatusCode
and then custom the reponse based on your own logic.如果您想自定义错误消息,您可以尝试使用
Middleware
检查Response.StatusCode
,然后根据您自己的逻辑自定义响应。
public void Configure(IApplicationBuilder app, IHostingEnvironment env, IServiceProvider serviceProvider)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseDatabaseErrorPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
app.Use(async (context, next) => {
//handle request
await next.Invoke();
//handle response
if (context.Response.StatusCode == StatusCodes.Status404NotFound)
{
//hanlde based on your logic
await context.Response.WriteAsync("Something wrong");
}
});
// Rest app.Use
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.