【问题标题】:Set Route Attribute with a String Parameter使用字符串参数设置路由属性
【发布时间】:2017-07-07 21:18:40
【问题描述】:

我正在尝试使用接受字符串参数的自定义路由创建一个 Web API 方法,但这似乎在应用程序启动时出错 - 所以我什至不需要点击应用程序的路由或方法打破。

// Pass String to TMS and Return an ID
[System.Web.Http.HttpGet]
[System.Web.Http.Route("api/job/import/{import:string}")]
public int TmsImport(string import)
{
    var TmsId = 0;
    if (import.Length > 0)
    {
        TmsId = 1;
    }
    return (TmsId);
}

应用程序在 Global.asx 中的以下方法上中断:

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

当我将一个 int 传递给该方法时,同样的方法有效,但它似乎无法识别参数中的名称“字符串”。我确实在这里看到了类似问题的答案:call to web api with string parameter - 但我无法弄清楚该答案的变化在哪里?是在 JS 的调用中还是在路由配置中?

【问题讨论】:

  • 只需从{import:string} 中删除:string 并尝试连接到“api/job/import/somestring”
  • 成功了,谢谢:)
  • 如果您不介意,我会将其作为答案发布
  • 是的,继续,我将其标记为正确

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


【解决方案1】:

你只需要从{import:string}中删除:string并尝试连接

“api/job/import/somestring”

MSDN article

【讨论】:

  • web-api 路由上的docs 也可能有助于了解其工作原理。
猜你喜欢
  • 1970-01-01
  • 2021-07-02
  • 2015-09-04
  • 2017-01-26
  • 2018-10-24
  • 2014-05-28
  • 2016-06-08
  • 1970-01-01
  • 2019-03-20
相关资源
最近更新 更多