Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

asp.net mvc c# getting the url of the page coming from

Tags:

c#

asp.net-mvc

i had a form in which i want a url of the page from which it got there. Like i am on /Content/Form and i got there from /Content/Document( i want to save this in my database) . what is the best way for this scenario?

like image 485
maztt Avatar asked Mar 12 '11 11:03

maztt


People also ask

What is ASP.NET MVC in C#?

Model View Controller (MVC) MVC is a design pattern used to decouple user-interface (view), data (model), and application logic (controller). This pattern helps to achieve separation of concerns.

Is C# and ASP.NET MVC are same?

They are the same thing. C# is the language you have used to do your development, but ASP.NET MVC is the framework you used to do it.

Is ASP.NET MVC still used?

It is no longer in active development. It is open-source software, apart from the ASP.NET Web Forms component, which is proprietary. ASP.NET Core has since been released, which unified ASP.NET, ASP.NET MVC, ASP.NET Web API, and ASP.NET Web Pages (a platform using only Razor pages).

What is .NET ASP.NET and C#?

Basically, ASP.NET is a web delivery mechanism that runs either C# or VB.NET in the background. C# is a programming language that runs ASP.NET as well as Winforms, WPF, and Silverlight.


2 Answers

HttpContext.Request.UrlReferrer

like image 190
Paul Creasey Avatar answered Oct 29 '22 06:10

Paul Creasey


The best way is to simply pass this information to the controller action.

So for example you could include the request url as a hidden field:

<% using (Html.BeginForm("Process", "SomeController")) { %>
    <%= Html.Hidden("requestUrl", Request.RawUrl) %>
    <input type="submit" value="OK" />
<% } %>

and inside the corresponding controller action :

[HttpPost]
public ActionResult Process(string requestUrl)
{
    // requestUrl will contain the url of the page used to 
    // render the form
    ...
}

You could also use the controller and action from route data:

<% using (Html.BeginForm("Process", "SomeController")) { %>
    <%= Html.Hidden("controllerName", ViewContext.RouteData.GetRequiredString("controller")) %>
    <%= Html.Hidden("actionName", ViewContext.RouteData.GetRequiredString("action")) %>
    <input type="submit" value="OK" />
<% } %>

and both controllerName and actionName will be sent in the POST request.

like image 26
Darin Dimitrov Avatar answered Oct 29 '22 06:10

Darin Dimitrov