Create url link to a controller from another controller - c#

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.

Related

.NET 6 best way to differentiate a route in the same controller class

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.

Route Prefix VS Controller Name ( Web api )

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

How to pass data to controller's field from URL?

I know how to send data to controller's action method as parameter from URL. Here I wonder how can I send data from URL to controller's field?
public MyAwesomeController : Controller {
public string SectionCode { get;set; }
}
and let's define Routes :
routes.MapRoute(
name : "AwesomeRouter",
url : "{code}/{action}",
defaults: new {controller = "MyAwesome", action = "Index", /* What to do here?*/}
);
I want SectionCode be filled with the {code} from URL. Is it possible to implement?
Yes it is, you can create inherited class from basic Controller class and override OnActionExecuting method where you can read url, route or any form data and store them in session or directly fill any field you need. Then create an inherited class of your controller.
public class MyAwesomeController : MyControllerBase
{
public ActionResult Index()
{
//this.SectionCode is available populated here
return View();
}
}
public class MyControllerBase : Controller
{
public string SectionCode
{
get;
private set;
}
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
this.SectionCode = filterContext.RequestContext.RouteData.Values["code"].ToString();
base.OnActionExecuting(filterContext);
}
}
Each time you hit any action in this controller using route definition you provided, field will be automatically populated. But when you will have more than one route defined they it could get easily into conflicts eg. when code will match to any controller name. Normal website should not work this way.
Your route should look like this:
routes.MapRoute(
name: "AwesomeRouter",
url: "{code}/{action}",
defaults: new { controller = "MyAwesome", action = "Index" }
);
The code should then be passed on to the action as a parameter. I am storing it in the view bag for explanation purposes:
public class MyAwesomeController : Controller
{
public ActionResult Index(string code)
{
ViewBag.Code = code;
return View();
}
}
Using this URL then:
http://somehost/4567/Index
If you access the Viewbag property in your view:
#ViewBag.Code
You should see:
4567

Map two different routes to the same controller action

I am working on extending the example at: https://docs.asp.net/en/latest/tutorials/first-web-api.html
They have a ToDo apis: /api/todo and /api/todo/{id}.
I want to extend it to ToDoGroups /api/ToDoGroup.
Under ToDoGroup, I want to reach a ToDo by the follwoing:
/api/ToDoGroup/{id}/ToDo/{id}.
How can I make it point to the same controller action? For example the following action below will also have another route like [HttpGet("ToDoGroup/{ToDoGroupid}/ToDo/{ToDoid}", Name = "GetTodo")]
[HttpGet("{id}", Name = "GetTodo")]
public IActionResult GetById(string id)
{
var item = TodoItems.Find(id);
if (item == null)
{
return NotFound();
}
return new ObjectResult(item);
}
first change the controller's route prefix:
[Route("api/todogroup/{groupId:int}")]
public class TodoController : Controller
then change your action's route:
[HttpGet("todo/{id}", Name = "GetTodo")]
public IActionResult GetById(int groupId, string id)
edit:
to get both routes, you can do this:
[Route("api")]
public class TodoController : Controller
[HttpGet("todo/{id}", Name = "GetTodo")]//api/todo/43
public IActionResult GetById(string id)
[HttpGet("todogroup/{groupdId:int}/todo/{id}", Name = "GetGroupTodo")]//api/todogroup/100/todo/43
public IActionResult GetById(int groupId, string id)
Asp.Net Web Api has a way of negating a route prefix (the route specified on the controller), but I cant find an equivalent in Asp.Net Core.

URL Routing in MVC C#

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
}
}

Categories

Resources