0

What I mean is - if user type something like:

Http://somepage.com/iNdex/sOmEaCtIoN

I would like to get /Index/SomeAction (or whatever it is called inside my controller). Since above URL is routed correctly by MVC engine, I guess there should be a way to do this.

EDIT:

I would like to get this in my View.

Edit 2:

I managed to solved my problem by passing type of the controller:

ViewContext.Controller.GetType()

and

Request.Url.AbsolutePath

to my method - and then using toLower() when searching for method match - still it would be better to simply get the name of controller and method that will be invoked by current request...

user2384366
  • 1,034
  • 2
  • 12
  • 28
  • Where exactly do you want to get this? There are way of getting the controller and action names. – Andrei V May 28 '14 at 11:11
  • I would like to get this inside my view, I need this in order to pass it to my BreadCrumbs generator. – user2384366 May 28 '14 at 11:13
  • Not sure exactly what you mean, but inside your action method you can use Request.Url to access properties of the URL –  May 28 '14 at 11:14
  • Try [this](http://stackoverflow.com/questions/362514/how-can-i-return-the-current-action-in-an-asp-net-mvc-view). – Andrei V May 28 '14 at 11:14
  • @AndreiV: I already tried that one - unfortunately it returns malformed URL... however now I started testing and it seem that simple ToLower() on method name should do the trick, since MVC can't see a difference between - for example: Index() and index() - I will investigate it further... – user2384366 May 28 '14 at 11:20
  • If you're trying to build links, then you can simply use `@Html.ActionLink`. – Andrei V May 28 '14 at 11:21
  • @AndreiV: Thanks but that is not what I'm trying to do ;] anyway I solved my problem by passing controller name and Request.Url path to my method, and then using ToLower(), but I would still prefer to simply get proper controller name and method that will be called... – user2384366 May 28 '14 at 12:02
  • Following the conventions, the class of the controller would require a "Controller" at the end of what you get using `ViewContext`. If you really need to compile and call the actions just like a normal method, you could use reflection. There are plenty examples of SO of calling a method by its name, using reflections. – Andrei V May 28 '14 at 12:05

1 Answers1

1

Just for answer your question, you can directly access the route values in your view through ViewContext:

@ViewContext.RouteData.Values["controller"] @*controller name*@
@ViewContext.RouteData.Values["action"] @*action name*@

So in your specific case you can do:

@string.Concat(ViewContext.RouteData.Values["controller"], "/", ViewContext.RouteData.Values["action"])

However personally I will still prefer to get those necessary value in controller and pass them into view through viewmodel.

tweray
  • 1,002
  • 11
  • 18
  • Unfortunately this will still give us malformed names, for my example it will return controller as "iNdex", and action as "sOmEaCtIoN". However, from what I can see - MVC engine works by ignoring action name letter size, so we cannot put "index()", and "INDEX()" in our controller since MVC cannot distinguish between the two. So I think the "solution" I posted is bearable, and I think You are correct as well - it could be better to pass controller and action name inside ViewBag in controller. – user2384366 May 30 '14 at 06:20