简体   繁体   English

MVC5控制器找不到使用“路线属性”的操作

[英]MVC5 Controller no action found using Route Attribute

I have the following MVC Controller: 我有以下MVC控制器:

[RoutePrefix("api/SystemCheck")]
public class SystemCheckController : ApiController
{
    [HttpGet]
    [Route("")]
    [Route("EnvironmentValidate")]
    [RequiresPrivilegeMVC((int)PrivilegeType.SystemCheck)]
    public IEnumerable<EnvironmentValidation> Get()
    {
        return FilteredEnvironmentValidate();
    }

I want to be able to access it these two ways: 我希望能够通过以下两种方式访问​​它:

  1. http://localhost/Perform/API/SystemCheck/EnvironmentValidate http:// localhost / Perform / API / SystemCheck / EnvironmentValidate
  2. http://localhost/Perform/API/SystemCheck/ http:// localhost / Perform / API / SystemCheck /

When I try option 2 I get a valid response. 当我尝试选项2时,我得到一个有效的响应。 However, when I try option 1 I get: 但是,当我尝试选项1时,我得到:

{ "Message": "No HTTP resource was found that matches the request URI ' http://localhost/Perform/API/SystemCheck/EnvironmentValidate '.", "MessageDetail": "No action was found on the controller 'SystemCheck' that matches the name 'EnvironmentValidate'." {“ Message”:“未找到与请求URI'http:// localhost / Perform / API / SystemCheck / EnvironmentValidate '相匹配的HTTP资源。”,“ MessageDetail”:“在控制器'SystemCheck'上未找到与名称“ EnvironmentValidate”匹配。” } }

Is it not finding my controller action because the method name is called Get but the route is specifying it as "EnvironmentValidate"? 是否由于方法名称称为Get而路由将其指定为“ EnvironmentValidate”而找不到我的控制器动作?

Here is how I have configured my RouteConfig.cs: 这是我配置RouteConfig.cs的方法:

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

        routes.MapMvcAttributeRoutes();

        routes.MapHttpRoute(
            name: "DefaultApiGet",
            routeTemplate: "api/{controller}/{action}",
            defaults: new { action = "Get" },
            constraints: new { httpMethod = new HttpMethodConstraint("GET") }
        );

Any ideas of what I'm missing? 有什么我想念的想法吗?

Thanks, 谢谢,

It looks like you are trying to use MVC attribute routing with a WebApi controller. 您似乎正在尝试将MVC属性路由与WebApi控制器一起使用。

The routes.MapMvcAttributeRoutes() is ignoring the attributes (since there is a mismatch between the namespace it is expecting), so only the DefaultApiGet route is getting mapped. routes.MapMvcAttributeRoutes()将忽略属性(因为期望的名称空间之间不匹配),因此仅映射了DefaultApiGet路由。

You can switch ApiController to Controller , so that you use the MVC controller, which matches the current attribute routing you are using (assuming that the Route attribute you are using is in the System.Web.Mvc namespace). 您可以将ApiController切换到Controller ,以便使用MVC控制器,该控制器与正在使用的当前属性路由匹配(假设正在使用的Route属性在System.Web.Mvc命名空间中)。

Or you can update the namespace to System.Web.Http which is the WebApi namespace, and call config.MapHttpAttributeRoutes() instead. 或者,您可以将命名空间更新为System.Web.Http (即WebApi命名空间),然后调用config.MapHttpAttributeRoutes()

More instructions here https://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2 for setting up WebApi attribute routing. 有关设置WebApi属性路由的更多说明, 参见https://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2

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

相关问题 调用控制器动作MVC5 - Calling a controller action MVC5 如何在MVC5中使用自定义属性路由重定向到路由 - How to redirect to route using custom attribute routing in MVC5 无法使用MVC5使用控制器名称和操作名称访问WebAPI - Unable to access WebAPI using Controller and Action name using MVC5 如果我在 mvc5 中将默认路由和属性路由设置为相同的 controller 则常规路由不起作用 - If I set default route and attribute routing to same controller in mvc5 then conventional routing is not working 如何在mvc5中使用allowhtml属性进行操作 - how to use allowhtml attribute for an action in mvc5 控制器中的MVC5模糊动作方法 - MVC5 ambiguous action methods in controller 使用ActionResult访问Action / Controller的MVC5被跳过,并产生“找不到资源”错误 - MVC5 accessing Action/Controller with ActionResult gets skipped and produces “The resource cannot be found” error 对同一控制器操作使用属性路由和全局路由 - Using attribute routing and a global route for the same controller action 将路由属性与 Html.Action C# MVC 一起使用 - Using Route Attribute with Html.Action C# MVC 如何使用甜蜜警报发布到控制器动作结果方法? MVC5 C# - How to post to an controller action result method using sweet alert? mvc5 c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM