简体   繁体   English

Asp.Net MVC 中 Web API 的默认控制器操作

[英]Default controller action for Web API in Asp.Net MVC

My Web API on an Asp.Net MVC web app is returning 404 error when receiving requests that don't specify any controller.我在 Asp.Net MVC Web 应用程序上的 Web API 在接收未指定任何控制器的请求时返回 404 错误。 The calls that are returning 404 error are:返回 404 错误的调用是:

https://myWebApp/api/ https://myWebApp/api/

The goal would be to handle these type of requests without returning error and return something like "true" or "new EmptyResult()".目标是处理这些类型的请求而不返回错误并返回诸如“true”或“new EmptyResult()”之类的内容。

Current routing for the Web API includes the following in WebApiConfig.cs Web API 的当前路由包括 WebApiConfig.cs 中的以下内容

public static class WebApiConfig
    {                
        public static void Register(HttpConfiguration config)
        {   
            config.Filters.Add(new IdentityBasicAuthenticationAttribute());

            config.MapHttpAttributeRoutes();

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }                
            );                
        }
    }

While I have routes explicitly defined for each API controller:虽然我为每个 API 控制器明确定义了路由:

[IdentityBasicAuthentication]
[Authorize]
public class myApi1Controller : ApiController
{
    [HttpGet]
    [Route("api/myApi1")]
    public string Get(string id) { ... }
}

I have tried to route these calls to a default API or MVC controller without success.我试图将这些调用路由到默认 API 或 MVC 控制器,但没有成功。

My current RouteConfig for the MVC app is:我当前用于 MVC 应用程序的 RouteConfig 是:

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
            namespaces: new[] { "myWebApp.Controllers" }
        );
    }
}

The order in which these are called is:调用它们的顺序是:

protected void Application_Start()
{                    
    AreaRegistration.RegisterAllAreas();                
    GlobalConfiguration.Configure(WebApiConfig.Register); 
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    RouteConfig.RegisterRoutes(RouteTable.Routes);
    BundleConfig.RegisterBundles(BundleTable.Bundles);
}

Create a controller to handle that route so it does not fail with not found创建一个控制器来处理该路由,因此它不会因未找到而失败

Like喜欢

[RoutePrefix("api")]
public class MyEmptyController : ApiController {
    //GET api
    [HttpGet]
    [Route("")]
    public IHttpActionResult Get() { 
        return StatusCode(HttpStatusCode.NoContent); //204
    }
}

Using attribute routing as it is already enabled via config.MapHttpAttributeRoutes();使用属性路由,因为它已经通过config.MapHttpAttributeRoutes();启用config.MapHttpAttributeRoutes(); in WebApiConfigWebApiConfig 中

Your route config is different from your WebApi config and I don't know the order that you are calling them.您的路由配置与您的 WebApi 配置不同,我不知道您调用它们的顺序。

in your RouteConfig:在您的 RouteConfig 中:

  url: "{controller}/{action}/{id}",

which means: http://localhost:PORTNUMBER/myApi1/ACTION这意味着: http://localhost:PORTNUMBER/myApi1/ACTION

in your WebApiConfig在您的 WebApiConfig 中

routeTemplate: "api/{controller}/{id}",

(method and id are optionals) which means: http://localhost:PORTNUMBER/api/myApi1/?id=value (method 和 id 是可选的)这意味着: http://localhost:PORTNUMBER/api/myApi1/?id=value

change your WebApiConfig, and you even will be able to avoid using the Route tag in your controller :更改您的 WebApiConfig,您甚至可以避免在控制器中使用 Route 标签:

  [IdentityBasicAuthentication]
    [Authorize]
    public class myApi1Controller : ApiController
    {
        [HttpGet]
        public string Get(string id) { 
    return "works!";
     }
    }

Edit: Keep every thing the same, Invoke your default GET method from: http://localhost:PORTNUMBER/api/myApi1编辑:保持所有内容相同,从以下位置调用默认的 GET 方法: http://localhost:PORTNUMBER/api/myApi1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM