【问题标题】:How to Pass QueryString in MVC WEBAPI routing for the following URL如何在 MVC WEBAPI 路由中为以下 URL 传递 QueryString
【发布时间】:2015-06-04 19:50:34
【问题描述】:

我有以下网址。当我通过传递值在浏览器中输入此 url 时,我无法访问我的应用程序中的 api 方法。我是 MVC 的新手。我不确定我们是否可以在 MVC WebApi 路由中传递 QueryString。如果可能,请帮助在 MVC WebAPI 路由中传递 QueryString。

http://localhost:1665/api/Load/LoadDetails?ID={1}&Latitude={2}&Longitude={3}&Uncertaint
y={4}&Street1={5}&Street2={6}&Neighborhood={7}&Ci&LocationDateTimeUTC={12} 

在 routeconfig 我有以下代码,

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

        config.Routes.MapHttpRoute(
            name: "DefaultApiId",
            routeTemplate: "api/{controller}/{id:int}",
            defaults: new { id = RouteParameter.Optional }
        );

        ReportsControllerConfiguration.RegisterRoutes(config);
        config.MapHttpAttributeRoutes();

在 ApiController 中

       [RoutePrefix("Load")]
        public class LoadController : ApiController
        {
        #region RESTful Methods

        [HttpGet]
        [Authorize()]     
        [Route("saveintransitcheckcall/{ID}/{Latitude}/{Longitude}/{Uncertainty}/{Street1}/{Street2}/{Neighborhood}/{City}/{State}/{Postal}/{Country}/")]
        public HttpResponseMessage SaveInTransitCheckCall(int ID)
        {}

请帮助访问该方法。提前致谢。

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-4 asp.net-web-api asp.net-mvc-routing


    【解决方案1】:

    如果你想通过如下url的查询字符串参数传递参数:

    http://localhost:1665/api/Load/LoadDetails?ID={1}&Latitude={2}&Longitude={3}&Uncertaint
    y={4}&Street1={5}&Street2={6}&Neighborhood={7}&Ci&LocationDateTimeUTC={12} 
    

    然后使用下面的代码:

    [HttpGet]
    [Authorize()] 
    [Route("saveintransitcheckcall")]
    public IHttpActionResult SaveInTransitCheckCall()
    {
      var queryString = this.Request.GetQueryNameValuePairs();
    
      var ID = queryString.SingleOrDefault(x => x.Key == "ID").Value;
      var Latitude = queryString.SingleOrDefault(x => x.Key == "Latitude").Value;
      //read other variables like above
    
    
      //do your remaining code here
    }
    

    否则你可以用 RESTful 方法尝试下面的代码:

    [HttpGet]
    [Authorize()]     
    [Route("saveintransitcheckcall/{ID:int}/{Latitude:decimal}/{Longitude:decimal}/{Uncertainty}/{Street1}/{Street2}/{Neighborhood}/{City}/{State}/{Postal}/{Country}/")]
    public HttpResponseMessage SaveInTransitCheckCall(int ID, decimal Latitude, decimal Longitude, string Uncertainty, string Street1, string Street2, string Neighborhood, string City, string State, string Postal, string country)
    {
      //do your remaining code here
    }
    

    请告诉我,这对你有用吗?

    【讨论】:

    • 是的,罗希特。我实际上尝试了第二种选择并为我工作。在 Route 中,如果提供像 {Latitude:decimal} 这样的参数类型,它实际上提供了一些错误。所以,我刚刚从路由中删除了所有参数,我只是像这样添加了 [Route("saveintransitcheckcall/")] 并在函数签名中添加了所有参数,例如 SaveInTransitCheckCall(int ID, decimal Latitude, decimal Longitude, string Uncertainty, string Street1,字符串 Street2,字符串 Neighborhood,字符串 City,字符串 State,字符串 Postal,字符串 Country)。感谢您的帮助。
    • @Vignesh 您可以从 {Latitude:decimal} 中删除 :decimal,这样它就会像 {Latitude}。这也适用于 REST ful API。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-09
    • 2020-08-04
    • 1970-01-01
    相关资源
    最近更新 更多