In .NET MVC, is there an easy way to check if I am on the main page?

I need to take a specific action if the 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 this value is the home page or not. In the example below, I check to see if the user is on a specific page ("Account / ResetPassword"). Is there a way to check if they are 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? Thank you so much!

+7
source share
4 answers

One way to get close to this is to look for a specific controller in RouteData . Assuming that the controller that you use for the home page is called "HomeController", then the RouteData for the request will contain the value "Home" for the key "Controller".

It will look something like this:

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

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

you will have:

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

and your controller will look like this:

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

This will eliminate the need to use .Contains()

Update :

You can also eliminate the need for a hidden field (and thereby reduce the total page weight for what will look like every page of your application) by matching the referrer URL ( Request.UrlReferrer.AbsoluteUri ) with the route. There is a message about this here.

How to get RouteData by url?

The idea would be to use the mvc mechanism to map the referrer URL to the MVC route in the LogOnInt method to completely isolate the code.

This will probably be cleaner than putting the controller name and the action name there so that the world sees along with the script in order to return it back to the server.

+7
source

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

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

Is it easy? :)

+5
source

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 the HtmlHelper or in the controller, which displays the input view.

Store the url in a hidden field like you do, and then in your post:

 [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 advantage of using Url.Action is that it will use your route table to generate the URL, that is, if your routes ever change, you do not have to change this code.

+3
source

you can use

  Request.Url.AbsoluteUri 

Then just check the string for the page name.

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

I got this method from this page:

How to get current page URL in MVC 3

There is another answer that may be useful to you.

0
source

All Articles