简体   繁体   English

.net core web api 控制器和动作选择

[英].net core web api controller and action selection

I need to generate appropriate 404 erors according to:我需要根据以下内容生成适当的 404 错误:

  1. If requesting controller doesn't exist;如果请求控制器不存在;
  2. If action doesn't exist;如果动作不存在;

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.

相关问题 混合web api动作和非api动作相同的控制器asp.net核心 - Mix web api action and non api action same controller asp.net core 具有许多参数的 .net 核心 API 控制器操作 - .net core API controller action with many parameters 如何在 ASP.Net Core 中实现自定义 controller 动作选择? - How to implement custom controller action selection in ASP.Net Core? 单元测试 .NET 核心 3 Web API Controller - Unit test .NET Core 3 Web API Controller 如何在 api mvc 项目 net core 3.1 中向 controller 添加操作 - How to add action to controller in api mvc project net core 3.1 将数组传递给asp net core web api action方法HttpGet - passing an array to a asp net core web api action method HttpGet 如何在 Web API .NET Core 中的控制器之外获取当前用户 - How to get current User outside a Controller in Web API .NET Core .NET Core(2.1)Web API动态命名到控制器操作的路由 - .NET Core (2.1) web API dynamic naming of routes to controller actions 如何在 .Net Core 3.1 Web API 控制器中获取声明? - How to get claims in .Net Core 3.1 Web API Controller? .net 核心 3 web api Z594C103F2C6E04C3D8AB059F031E0 执行后执行代码 - .net core 3 web api controller - code execution after sending response
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM