Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Attribute Routing for querystring

I have below route URL:-

www.domanname.com/subroute/GetInfo?param1=somestring&param2=somestring

I have function in webapi as:-

public class HomeController : ApiController
{
    public object GetInfo(string param1,string param2)
    {}
}

To apply route:-

[RoutePrefix("subroute")]
public class HomeController : ApiController
{
    [Route("GetInfo?param1={param1:string}&param2={param2:string}")]
    public object GetInfo(string param1,string param2)
    {}
}

But after applying above URL:-

www.domanname.com/subroute/GetInfo?param1=somestring&param2=somestring

It is not able to find that URL

How can I design this particular route?

like image 680
C Sharper Avatar asked May 14 '18 11:05

C Sharper


People also ask

What is attribute routing?

As the name implies, attribute routing uses attributes to define routes. Attribute routing gives you more control over the URIs in your web API. For example, you can easily create URIs that describe hierarchies of resources. The earlier style of routing, called convention-based routing, is still fully supported.

How do you pass a QueryString?

To pass in parameter values, simply append them to the query string at the end of the base URL. In the above example, the view parameter script name is viewParameter1.

What is QueryString in URL?

A query string is a set of characters tacked onto the end of a URL. The query string begins after the question mark (?) and can include one or more parameters. Each parameter is represented by a unique key-value pair or a set of two linked data items. An equals sign (=) separates each key and value.

What is request QueryString ()?

The value of Request. QueryString(parameter) is an array of all of the values of parameter that occur in QUERY_STRING. You can determine the number of values of a parameter by calling Request. QueryString(parameter). Count.


1 Answers

You need to modify the routes a bit as query string are not normally used in attribute routes. They tend to be used for inline route parameters.

[RoutePrefix("subroute")]
public class HomeController : ApiController {
    //Matches GET subroute/GetInfo?param1=somestring&param2=somestring
    [HttpGet]
    [Route("GetInfo")]
    public IHttpActionResult GetInfo(string param1, string param2) {
        //...
    }
}

Also

Enabling Attribute Routing

To enable attribute routing, call MapHttpAttributeRoutes during configuration. This extension method is defined in the System.Web.Http.HttpConfigurationExtensions class.

using System.Web.Http;

namespace WebApplication
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // Web API routes
            config.MapHttpAttributeRoutes();

            // Other Web API configuration not shown.
        }
    }
}

Reference Attribute Routing in ASP.NET Web API 2

like image 119
Nkosi Avatar answered Oct 14 '22 23:10

Nkosi