12

I need to take a particular action if a user logs in from the home page. In my LogOnModel, I have a hidden field:

@Html.Hidden("returnUrl", Request.Url.AbsoluteUri)

In my Controller, I need to check if that value is the Home page or not. In the example below, I'm checking to see if the user is on a particular page ("Account/ResetPassword"). Is there a way to check to see if they're on the home page without resorting to regular expressions?

    [HttpPost]
    public ActionResult LogOnInt(LogOnModel model)
    {
       if (model.returnUrl.Contains("/Account/ResetPassword"))
       {
          return Json(new { redirectToUrl = @Url.Action("Index","Home")});
       }

Any ideas? A million thanks!

Hairgami_Master
  • 5,429
  • 10
  • 45
  • 66

4 Answers4

8

One way to approach this would be to look for the specific controller in the RouteData. Assuming that the controller you are using for the home page is called "HomeController", then the RouteData for the request would contain the value "Home" for the key "Controller".

It would look something like this:

instead of (or in addition to if you have other reasons for it):

 @Html.Hidden("returnUrl", Request.Url.AbsoluteUri)

you would have:

 @Html.Hidden("referrer", Request.RequestContext.RouteData.Values['Controller'])

and your controller would look like:

[HttpPost]
public ActionResult LogOnInt(LogOnModel model)
{
   if (model.referrer = "Home")
   {
      return Json(new { redirectToUrl = @Url.Action("Index","Home")});
   }
 }

This would eliminate the need for using .Contains()

Update:

You could also eliminate the need for a hidden field (and thereby reduce overall page-weight for what would seem like every page in your application) by mapping the referrer url (Request.UrlReferrer.AbsoluteUri) to a route. There is a post about that here.

How to get RouteData by URL?

The idea would be to use the mvc engine to map a referrer url to an MVC route in the LogOnInt method, allowing the code to be entirely self contained.

This would probably be cleaner than putting the controller name and action name out there for the world to see along with scripting to push it back to the server.

Community
  • 1
  • 1
parKing
  • 677
  • 3
  • 13
  • thanks! I'm going to give that a shot. I'm assuming when I'm on the main page (Home/Index) it will give me that value, though the actual user URL doesn't display it? – Hairgami_Master Jun 27 '12 at 21:56
  • To get the controller name you'll have to access the Values property on the RouteData object like this: Request.RequestContext.RouteData.Values["Controller"] – Rafe Jun 27 '12 at 22:37
  • @Rafe thanks for pointing that out, I didn't even notice that mistake. – parKing Jun 28 '12 at 03:56
  • @Hairgami_Master Assuming you had a controller called "HomeController" and an action called "Index", RouteData.Values["Controller"] would only return the "Home", however, you can access the action with RouteData.Values["Action"] – parKing Jun 28 '12 at 03:58
6

In any view, the following code returns the current controller name.

@ViewContext.Controller.ValueProvider.GetValue("controller").RawValue.ToString()

Is it easy enough? :)

Tohid
  • 6,175
  • 7
  • 51
  • 80
  • 1
    @Tohid- thanks a million, I think that may be what I was looking for. Also, I'm assuming I can also specify "action" as a value as well. – Hairgami_Master Jun 27 '12 at 22:00
5

You can get the current URL via

string controller = (string)ViewContext.RouteData.Values["controller"];
string action = (string)ViewContext.RouteData.Values["action"];
string url = Url.Action(action, controller);

You can do this in an HtmlHelper or in the controller that renders the login view.

Store url in a hidden field as you did, then in your post action:

[HttpPost]
public ActionResult LogOnInt(LogOnModel model)
{
   // Create your home URL
   string homeUrl = Url.Action("Index", "Home");
   if (model.referrer == homeUrl)
   {
      return Json(new { redirectToUrl = @Url.Action("Index","Home")});
   }
 }

The benefit of using Url.Action is that it will use your route table to generate the URL, meaning if your routes ever change, you won't have to change this code.

Omar
  • 39,496
  • 45
  • 145
  • 213
0

You could use the

  Request.Url.AbsoluteUri

Then just check the string for the page name.

Might not be the best way, but its a quick and easy way.

I got that method from this page:

How to get current page URL in MVC 3

There is also another answer there that may be useful for you.

Community
  • 1
  • 1
Gaz Winter
  • 2,924
  • 2
  • 25
  • 47