简体   繁体   English

如何知道 ASP.NET 中 controller 索引的调用者路由?

[英]How to know the caller route for Index of a controller in ASP.NET?

So I have a something like this in my cshmtl:所以我的 cshmtl 中有这样的东西:

<div>
    <a href="/cars"></a>
</div>
<div>
    <a href="/bikes"></a>
</div>

So it goes to cars controller and hits the function below:所以它去了汽车 controller 并击中下面的 function:

[HttpGet]
[Route("cars")]
[Route("bikes")]
public ActionResult Index()
{
    //here I want to set up if else depending on if cars got called or bikes
    return View();
}

In the index function, I wanna know through some if else which route got called.在索引 function 中,我想通过一些其他方式知道调用了哪条路线。 Maybe using view bag or something?也许使用view bag之类的?

The request context should have all the information you need.请求上下文应该包含您需要的所有信息。

[HttpGet]
[Route("cars")]
[Route("bikes")]
public ActionResult Index()
{
    var requestUri = Context.Request.Url.AbsoluteUri;

    return View();
}

Here are the relevant documentation pages:以下是相关文档页面:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM