How to create route like site.net/query?
Attention, not
site.net/?q=querystring
What I want is:
site.net/querystring
I tried something like this, but this don't work
[Route("")]
class MyController : Controller
{
[HttpGet]
public string Get()
{
return Request.QueryString.ToString();
}
Just put that path in the routing string:
[HttpGet("/Something/[action]/{q}")]
public string Get(string q)
{
return q;
}
When you access this link: localhost/Something/Get/mynicestring
mynicestring will be shown
Related
I have an API controller say TestController.
I have the following route definition: api/[controller] on class level and I can call the default get method as follows: .../api/test
I want to have another method in the same controller, I also want to call it with Get and the link should be as follows: .../api/test-abc
What is the best way to differentiate this second method within the same controller class.
For test-abc, you can configure the Route attribute as ~/api/test-abc to override the controller base route. See this link for details.
[Route("api/[controller]")]
public class TestController : ControllerBase
{
public IActionResult Get()
{
// ...
}
[HttpGet]
[Route("~/api/test-abc")]
public IActionResult GetAbc()
{
// ...
}
}
This approach changes only the URL of the "test-abc"-action; all other actions of the controller use the base URL configured on class level.
In .Net 6 by following Minimal API's
app.MapGet("api/test", () =>
{
return "";
});
app.MapGet("api/test-abc", () =>
{
return "";
});
By following Traditional Method
[Route("api")]
public class TestController : ControllerBase
{
[HttpGet("test")]
public IActionResult Get()
{
// ...
return Ok();
}
[HttpGet("test-abc")]
public IActionResult GetAbc()
{
// ...
return Ok();
}
}
You can by defining the controllers route template like as below:
[Route("api/[controller]")]
public class TestController : Controller
{
...
}
So you can define a method like:
[HttpGet]
[Route("~/api/Test/Index")]
public IActionResult Index()
{
return Foo.Bar();
}
[HttpGet]
[Route("~/api/Test/Index2")]
public IActionResult Index2()
{
return Foo.Bar2();
}
[HttpGet]
public IActionResult Index3()
{
return Foo.Bar3();
}
You can call it by ../api/test/index2 to use the method Index2 or ../api/test to use Index3.
This is for the standardized way to do, in your case you would use - instead of / at the Route("...") attribute.
If you use - you need to call ../api/test-index2
For further information take a look at documentation.
I develop a WebApi RESTful in ASP.NET Core 2.0.
From an action method in a controller A I want to create the URL address for an action method located in a different controller B:
[Route("api/applications/{idbcon}/bcontrollers")]
public class BController : Controller{
[HttpGet({idbcon}), Name = "ActionB"]
public IActionResult ActionB(int idbcon, [FromHeader(Name = "Accept")] string mediatype){
return OK();
}
}
[Route("api/applications/{idapp}/bcontrollers/{idbcon}/acontrollers")]
public class AController: Controller{
[HttpGet(), Name = "GetURLBController"]
public IActionResult GetURLBController()
{
var url = /* Here I would like to get the link to "ActionB" that belong to a Controller "BController" */;
return Ok(url);
}
}
Any advice ???
Use the IUrlHelper.Action overload that takes action and controller parameters:
public class AController : Controller
{
[HttpGet]
public IActionResult GetURLBController()
{
var url = Url.Action("ActionB", "BController");
return Ok(url);
}
}
IUrlHelper is something you inherit from the Controller base class, exposed as the Url property.
I was wondering that if we use RoutePrefix attribute in our web api controller with a different name from controller's actual name. So would it work or not?
As far as i did
[RouterPrefix("quotation")]
public class SaleOrderController : ApiController { ... }
if we define RoutePrefix like above we can't access it via /quotation but we can access it using saleorder.
So what is RoutePrefix for or am i doing something wrong ?
To use default route use Route("")
[RoutePrefix("quotation")]
public class SaleOrderController : ApiController {
//GET quotation
[Route("")]
[HttpGet]
public IHttpActionResult GetAll() { ... }
}
Source: Attribute Routing in ASP.NET Web API 2 : Route Prefix
In order for it to work, you need to call the code below inside your WebApiConfig.Register() method:
config.MapHttpAttributeRoutes();
So your RoutePrefix works as exptected:
[RoutePrefix("quotation")]
public class SaleOrderController : ApiController
{
[Route("example")]
[HttpGet]
public IHttpActionResult Example()
{
return Ok();
}
[Route("another")]
[HttpGet]
public IHttpActionResult Another()
{
return Ok();
}
}
So your could access your apis like this:
quotation/example
quotation/another
I am defining my application URLs like
domainname.com/storecontroller/storeaction/storename
I want to rewrite like
domainname.com/storecontroller/storename
Actually, I need to skip storeaction from the url, I don't want to do it with querystring with "?" Is there anyway to do it by registering rout config path or any another way?
Yes. You can define action as a default parameter, and match for only specific controllers with a Regex constraint:
routes.MapRoute("<route name>", "{controller}/{storename}",
new
{
action = "storeaction"
},
new
{
controller = "somecontroller1|somecontroller2|somecontroller3",
});
(Action will always have the default value "storeaction")
Note that you need to define this route before the default generic route so it doesn't catch it before this kicks in.
Using Attribute routing
[RoutePrefix("Home")]
public ActionResult HomeController : Controller
{
[Route("{storeName}")]
public ActionResult ProcessStore(string storeName)
{
// to do : Return something
}
public ActionResult Index()
{
// to do : Return something
}
}
[RoutePrefix("Payment")]
public ActionResult PaymentController : Controller
{
[Route("{storeName}")]
public ActionResult ProcessStore(string storeName)
{
// to do : Return something
}
}
I have an url like this:
http://localhost:9562/Account/LogOn?ReturnUrl=%2fCabinet%2fCabinet
I need to parse it to this:
Cabinet/Cabinet
I've looked through
this and this but i can't understand how to use it with my example.
The easiest way would be to accept it as a parameter in your LogOn action:
public class AccountController : Controller
{
public ActionResult LogOn(string ReturnUrl = "")
{
}
}
Note, providing a default value (i.e. = "") allows the action to execute even if the query parameter isn't present in the request.
Alternatively, you could access it through the Request property of your controller:
public class AccountController : Controller
{
public ActionResult LogOn()
{
string request = this.Request.QueryString["ReturnUrl"];
}
}
Try this:
string r = Request.QueryString["ReturnUrl"].Substring(1);
Response.Write(r);