ASP.NET MVC 4 - Redirect to the same page after controller ends

actionresult stay on the same page
how to redirect to another page in mvc
how to stay on the same page after submit in asp net
stay on same page after form submit c#
redirect to action with model in mvc
redirecttoaction works like in asp net mvc c# as
response.redirect in mvc
how to redirect from view to controller in mvc

From a page I have the following:

@using (Html.BeginForm("AddEntry", "Configure", FormMethod.Get, new { returnUrl = this.Request.RawUrl }))
{
    @Html.TextBox("IP")
    @Html.Hidden("TypeId", 1)
    <input type="submit" value="@Resource.ButtonTitleAddComponent" />
}

so controller is called correctly:

public ActionResult AddEntry(string ip, int TypeId, string returnUrl)
{
    // Do some stuff

    return Redirect(returnUrl);
}

My problem is that returnUrl gets null and it does not redirect to the same page that called the controller. Ideas?

Using: ASP.NET MVC 4 Razor

You could use a Request.QueryString method to get some values from URL, for sample:

@using (Html.BeginForm("AddEntry", "Configure", FormMethod.Get, null))
{
    @Html.TextBox("ip")
    @Html.Hidden("TypeId", 1)
    @Html.Hidden("returnUrl", this.Request.RawUrl)
    <input type="submit" value="@Resource.ButtonTitleAddComponent" />
}

And in your controller, receive it as a parameter string returnUrl.

c ASP.NET MVC 4 Redirect to the same page after controller ends , c ASP.NET MVC 4 Redirect to the same page after controller ends? return Redirect(Request.UrlReferrer.PathAndQuery);. I am on page 'A', from there I navigate� Browse other questions tagged asp.net-mvc asp.net-mvc-3 redirect controller action or ask your own question. The Overflow Blog How Stack Overflow hires engineers

you can also do this if you need to return to something like details page and return to the same page with a query:

return Redirect(Request.UrlReferrer.PathAndQuery);

Staying on the same page after submit, I have a form with submit button and when i press it then blank page is showing but I want it NET/MVC/Staying on the same page after submit where "action" and "controller" will take them back to the original url then you can return to same page by returning same View to the action result for example If you create an ASP.NET MVC 3 or 4 Internet Application project, it'll have a complete example of how to use return url's when authenticating. When you add the AuthorizeAttribute to a controller to force authentication, it'll redirect the user to your Login method, and automatically append the returnUrl parameter.

You can get the Refer URL from the Request in the controller:

public ActionResult AddEntry(string ip, int TypeId, string returnUrl)
{

     // Do some stuff
     string url = this.Request.UrlReferrer.AbsolutePath;

     return Redirect(url);
}

This will redirect you exactly to the calling URL.

ASP.NET MVC, There are many ways for returning or rendering a view in ASP.NET MVC. Many developers got confused when to use View(), RedirectToAction(), Redirect() and OFF: Get Membership + 3PCS (Personal Career Services) Offer Ending in a redirect URL to a specific action/controller in your application and� 2) In a Controller or base Controller event eg Initialze, I want to redirect to another page (AbsoluteRootUrl + Controller + Action) Why would you want to redirect from a controller init? the routing engine automatically handles requests that come in, if you mean you want to redirect from the index action on a controller simply do:

in your controller class use Request.UrlReferrer. There's no need to pass the url from the page.

   public ActionResult AddEntry(string ip, int TypeId)
    {

         // Do some stuff

         return Redirect(Request.UrlReferrer.ToString());
    }

Difference between return View() ,return Redirect() and return , Example : public ActionResult Index() { UserDetailModel obj = new In this above code the view is (it never call any controller) which is referring the In this above,it will generate the URL for you based on your route table. 3) Redirect also work same as RedirectToAction , but you have to mention full URL to redirect. I'd like show a form with some field (one in the example), submit it, save and display the same page with a reset of all fields. The probelm when I submit, I go the "Save" action but when I display the view the form is still filled in. The model : public class TestingModel { public string FirstName { get; set; } } The controller :

@using (Html.BeginForm("AddEntry", "Configure", new { returnUrl = this.Request.RawUrl }))
{
    @Html.TextBox("IP")
    @Html.Hidden("TypeId", 1)
    <input type="submit" value="@Resource.ButtonTitleAddComponent" />
}

Change your code like this

Basic code in MVC to redirect from one page to another, Basic code in MVC to redirect from one page to another. http://stackoverflow. com/questions/32153594/redirect-to-another-page-in-asp-net-mvc4 you click on that button it will redirect to Index2 page of same controller. Browse other questions tagged c# asp.net-mvc redirect controller actionresult or ask your own question. The Overflow Blog Podcast – 25 Years of Java: the past to the present

HttpResponse.Redirect Method (System.Web), client to a new URL. Specifies the new URL and whether execution of the current page should terminate. The following example forces an unconditional redirection to another Web site. C# Copy For more information, see the End method. Note ASP.NET performs the redirection by returning a 302 HTTP status code. ASP.NET MVC Questions and Answers. ASP.NET MVC is an open source and lightweight web application development framework from Microsoft. This book has been written to prepare yourself for ASP.NET MVC Interview. This book is equally helpful to sharpen their programming skills and understanding ASP.NET MVC in a short time.

ControllerBase.RedirectToAction Method (Microsoft.AspNetCore.Mvc), Redirects (Status302Found) to an action with the same name as current one. The 'controller' and 'action' names are retrieved from the ambient values of the current request. The created RedirectToActionResult for the response. Attributes Applies to. ASP.NET Core. 3.1 3.0 2.2 2.1 2.0 1.1 1.0. Is this page helpful? Yes No. ASP.NET MVC Redirect ASP.NET MVC Passing data ASP.NET MVC Output ASP.NET MVC Route Redirect user to another page from controller action method in ASP.NET MVC

Various ways of redirecting a request in ASP.NET Core , To that end this article discusses them with examples. NET Core 3.1 with mini project : MVC, Razor Pages, Web API, Entity Framework Core, and Blazor. For example, consider the following two actions of HomeController. will use the same HTTP method (and body) while redirecting to the target URL. ASP.NET MVC 4-Redirect to the same page after controller ends (4) From a page I have the following: @using (Html.BeginForm("AddEntry", "Configure", FormMethod.Get, new { returnUrl = this.Request.RawUrl })) { @Html.TextBox("IP") @Html.Hidden("TypeId", 1) <input type="submit" value="@Resource.ButtonTitleAddComponent" /> }

Comments
  • In the controller, parameter returnUrl continues being null with this attempt. What I want is: I have a view that has the code indicate, when I submit this values to the controller, I want that once the controller ends it returns to the same page from where submit was invoked.
  • Also, check the html output to see if the View is getting the value from URL with Request.QueryString.
  • @user1624552 Replace that QueryString with this.Request.RawUrl. @FelipeOriani the URL to redirect is the current URL from the requesting form.
  • As @AndreCalil says, it is this.Request.RawUrl instead of QueryString. It works! :) passing QueryString, the controller receives an empty string. Also,no need to pass null at the end of Html.BeginForm.
  • As you answered first and your solution works ok, I have voted you as the accepted answer. Also, as my question was about passing a parameter to return to the caller page, I have choosen your answer as the accepted. Other solutions commented here also work, and maybe they are more cleaner because there is no need to pass an extra parameter.
  • I am on page 'A', from there I navigate to page 'B'. Now I Refresh page 'B'. In such cases, Request.UrlReferrer return 'B', however I am expecting 'A'. Any Help??
  • This may not work depending on your "Post Redirect Get" strategy. If you have a validation error on the form, the UrlReferrer may end up being the page itself.
  • Your solution also works like a charm. no need to pass returnUrl to the controller from the page.
  • Also, Request.UrlReferrer.AbsoluteUri works. What is the difference between Request.UrlReferrer.AbsoluteUri and Request.UrlReferrer.AbsolutePath? Both work.
  • You should use it carefully, it may be [null] (stackoverflow.com/questions/149130/request-urlreferrer-null).
  • To handle if its null I just picked a default action it should go to if I don't have a referrer, this works a treat.
  • Request.UrlReferrer.AbsoluteUri will concatenate querystrings eg if the first attempt has yoururl.com?msg=error and the action is repeated and this time successful in this view the redirect will become yoururl.com?msg=error?msg=success so I recommend using AbsolutePath
  • There a error there! UrlReferrer is an Uri, you should pass string to Redirect.
  • Right, I was not on the computer to check it, fixed
  • There's no need to convert to string. By doing Request.UrlReferrer.AbsoluteUri it works.