MTplus
MTplus

Reputation: 2391

How to route a string in .net core

I have a controller in my ASP.NET Webb app that should display search results when I search like this..

Cases/Search/Volvo

In this case Volvo is the string that I searched for.

It currently work when searching like this..

Cases/Search?q=volvo

How should my routing look like for this to work?

Upvotes: 2

Views: 2869

Answers (1)

Nkosi
Nkosi

Reputation: 247163

You can consider using Attribute routing

[Route("[controller]"]
public class CasesController : Controller {

    //Matches GET cases/search/volvo
    [HttpGet]
    [Route("search/{q}")]
    public IActionResult Search(string q) {
        //...code removed for brevity
    }

}

Reference Routing to Controller Actions

Upvotes: 3

Related Questions