ASP.net Web API端点未命中

时间:2019-05-23 09:14:26

标签: c# asp.net-web-api

我在asp.net webapi中创建了一个接受两个参数的终结点。我正在使用邮递员进行测试,但是端点没有命中。我还有其他无法传递参数的方法。不确定为什么会出现问题。

我尝试在邮递员中使用以下网址调用端点

http://localhost:56888/api/terms/reviewlegalfundclass/11166/2

我还将属性路由更改为  [Route(“ api / terms / {fundclassId} / {reviewtypeId}”)]

我看到的作品就是这种组合

  [Route("api/terms/reviewlegalfundclass/{id}")]
  http://localhost:56888/api/terms/reviewlegalfundclass/11166 - postman call

以下是在webapi.config中配置路由的方式

 public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.Routes.MapHttpRoute(
            name: "WebApi",
            routeTemplate: "api/{controller}/{action}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );


        config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));

        var appXmlType = config.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault(t => t.MediaType == "application/xml");
        config.Formatters.XmlFormatter.SupportedMediaTypes.Remove(appXmlType);

        config.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;

        config.Formatters.JsonFormatter.SerializerSettings.Converters.Add(new Newtonsoft.Json.Converters.StringEnumConverter());

        config.Formatters.Add(new BsonMediaTypeFormatter());
    }
}

控制器

[System.Web.Http.HttpPut]
[SkipTokenAuthorization]
[Route("api/terms/reviewlegalfundclass/{fundClassId}/{reviewTypeId}")]
public string ReviewLegalFundClass(int fundClassId, int reviewTypeId)
{
    var svc = Resolve<IUserIdentityService>();
    var user = svc.GetCurrentUser();

    try
    {
        var fcService = GetService<LEGAL_FUND_CLASS>();
        var fundClass = fcService.Get(fundClassId);
        var response = "N/A";
        var users = GetViewService<V_PERMAL_USER_ALL>().GetAll();

        if (reviewTypeId == (int)LegalFundClassReviewType.Fees)
        {
            fundClass.FEES_LAST_REVIEWED_BY_ID = user.ID;
            fundClass.FEES_LAST_REVIEWED_DATE = DateTime.Now;

            var feeUsr = users.FirstOrDefault(x => x.ID == fundClass.FEES_LAST_REVIEWED_BY_ID);
            response = $"Last Reviewed: {feeUsr?.LOGIN_NAME} {fundClass.FEES_LAST_REVIEWED_DATE.Value.ToString(PermalConstants.DateFormat2)}";
        }
        else if (reviewTypeId == (int)LegalFundClassReviewType.Terms)
        {
            fundClass.TERMS_LAST_REVIEWED_BY_ID = user.ID;
            fundClass.TERMS_LAST_REVIEWED_DATE = DateTime.Now;

            var termsUsr = users.FirstOrDefault(x => x.ID == fundClass.TERMS_LAST_REVIEWED_BY_ID);
            response = $"Last Reviewed: {termsUsr?.LOGIN_NAME} {fundClass.TERMS_LAST_REVIEWED_DATE.Value.ToString(PermalConstants.DateFormat2)}";
        }
        else throw new Exception("Invalid fund class review type");

        fcService.Update(fundClass);

        return response;
    }
    catch (Exception ex)
    {
        throw new Exception(ex.Message);
    }
}

0 个答案:

没有答案