gpt4 book ai didi

c# - 使用字符串参数设置路由属性

转载 作者:行者123 更新时间:2023-11-30 12:39:36 24 4
gpt4 key购买 nike

我正在尝试使用接受字符串参数的自定义路由创建 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 传递给该方法时,同样的方法有效,但它似乎无法识别参数中的名称“string”。我确实在这里看到了类似问题的答案:call to web api with string parameter - 但我无法弄清楚该答案的变化在哪里?它是在 JS 的调用中还是在路由配置中?

最佳答案

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

"api/job/import/somestring"

MSDN article

关于c# - 使用字符串参数设置路由属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44970544/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com