带有参数的属性路由不起作用:没有参数正在起作用

Attribute routing with parameter is not working: Without Parameter is working

我正在研究具有属性路由的 Asp.Net MVC 5 Web API 项目。我有一个应用了 RoutePrefix 和 Route 的国家/地区控制器。如果我确实请求了一个没有参数的方法或一个模型作为参数的方法,它工作正常。

例如,我请求

AddorEdit 方法使用 Country model 来自 http://localhost/api/Master/Country/AddOrEdit,

GetAll 方法来自 http://localhost/api/Master/Country/GetAll

这项工作和 return 结果。

但是,如果我用字符串参数调用一个方法,它不起作用。 例如,

如果我打电话给

Get/{transId} 来自 http://localhost/api/Master/Country/Get/1

Get/{transId} 来自 http://localhost/api/Master/Country/Get/?transId=1

Get/{transId} 来自 http://localhost/api/Master/Country/Get?transId=1,

这不起作用。这会产生以下错误:

<Error>
    <Message>
         No HTTP resource was found that matches the request URI 'http://localhost:11035/api/Master/Country/Get/449cc9b8-81b3-4b7e-8561-b98cf39d9492'.
    </Message>
    <MessageDetail>
         No action was found on the controller 'Country' that matches the request.
    </MessageDetail>
</Error>

我还使用 HTTP 动词进行了检查并应用了 2 个动词。另外,从 Postman 发出 POST 请求,但仍然收到相同的错误。

我已经尝试 google 和 SO 但没有成功。我还添加了默认路由规则仍然没有运气。

路由代码

    config.MapHttpAttributeRoutes();

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

控制器代码

    [RoutePrefix("api/Master/Country")]
    public class CountryController : BaseApiController
    {
        [Route("AddOrEdit")]
        [HttpPost]
        public IHttpActionResult AddOrEdit(Country country)
        {
            try
            {
                using (ApplicationDbContext dbContext = new ApplicationDbContext())
                {
                    var _country = dbContext.Country.Where(x => x.TransID.Equals(country.TransID)).FirstOrDefault();

                    if(null == _country)
                        dbContext.Country.Add(country);
                    else
                    {
                        _country.Code = country.Code;
                        _country.Name = country.Name;
                        _country.Description = country.Description;
                        _country.IsDeleted = country.IsDeleted;
                    }

                    dbContext.SaveChanges();
                }

                return Ok();
            }
            catch (Exception e)
            {
                return InternalServerError(e);
            }
        }


        [Route("Get/{ transId }")]
        [HttpGet, HttpPost]
        public IHttpActionResult GetCountryResult(string transId)
        {
            Country data;

            try
            {
                using (ApplicationDbContext dbContext = new ApplicationDbContext())
                {
                    data = dbContext.Country
                        .Where(x => x.TransID.Equals(new Guid(transId)))
                        .FirstOrDefault();
                }

                return Ok(data);
            }
            catch (Exception e)
            {
                return InternalServerError(e);
            }
        }


        [Route("GetAll")]
        [HttpGet]
        public IHttpActionResult GetCountryResults()
        {
            List<Country> lidata;

            try
            {
                using (ApplicationDbContext dbContext = new ApplicationDbContext())
                {
                    lidata = dbContext.Country.ToList();
                }

                return Ok(lidata);
            }
            catch (Exception e)
            {
                return InternalServerError(e);
            }
        }
    }

尝试删除路由中 transId 处的多余空格 例如: 代替这个 [Route("Get/{ transId }")] 试试这个 [Route("Get/{transId}")]

查看路线差异

[Route("Get/{ transId }")]

[Route("Get/{transId}")]

  [Route("Get/{transId}")]
    [HttpGet, HttpPost]
    public IHttpActionResult GetCountryResult(string transId)
    {
        Country data;

        try
        {
            using (ApplicationDbContext dbContext = new ApplicationDbContext())
            {
                data = dbContext.Country
                    .Where(x => x.TransID.Equals(new Guid(transId)))
                    .FirstOrDefault();
            }

            return Ok(data);
        }
        catch (Exception e)
        {
            return InternalServerError(e);
        }
    }