user3378165
user3378165

Reputation: 6896

Get URL of actual page in MVC Controller

On my MVC project I have a 'Contact Us' form on the footer of the Layout page so the 'Contact Us' form appears on each page of the website.

I have to check each time the form is submitted, on which page was it, I mean the url of the page that submitted the 'Contact Us' form in its POST method.

For example on the homepage:

http://www.test.com/myWebSite.Site/Home

I need the get: Home

But the problem is that when I submit the form with a POST request, in the Action, Request.Url always gives me that: (The root of the Controller and the Action..)

http://www.test.com/myWebSite.Site/Shared/SubmitContactForm

I think that what I need is the URL of the step before... I am not sure how to do that, any idea?

Upvotes: 0

Views: 1374

Answers (1)

Travis Acton
Travis Acton

Reputation: 4430

Just a thought (if I understand your question correctly, apologies if not):

In your layout page contact form, add two hidden inputs to pass in and use:

<input type="hidden" name="currentAction" value="@ViewContext.RouteData.Values["Action"].ToString()">

<input type="hidden" id="currentController" value="@ViewContext.RouteData.Values["Controller"].ToString()">

this will give you the exact controller and action served even though your form is located on the shared layout view

Upvotes: 1

Related Questions