ASP.NET MVC 4 - 在控制器结束后重定向到同一页面

时间:2022-05-12 16:47:22

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?

我的问题是returnUrl获取null并且它不会重定向到调用控制器的同一页面。想法?

Using: ASP.NET MVC 4 Razor

使用:ASP.NET MVC 4 Razor

6 个解决方案

#1


10  

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

您可以使用Request.QueryString方法从URL获取一些值,用于示例:

@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.

在您的控制器中,将其作为参数字符串returnUrl接收。

#2


31  

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);

#3


10  

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

您可以从控制器中的Request获取Refer URL:

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.

这会将您重定向到调用URL。

#4


8  

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

在您的控制器类中使用Request.UrlReferrer。没有必要从页面传递URL。

   public ActionResult AddEntry(string ip, int TypeId)
    {

         // Do some stuff

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

#5


0  

@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

像这样更改你的代码

#6


-1  

  1. on Get like Edit(int? id)

    在Get上编辑(int?id)

    ViewBag.RefUrl = Request.UrlReferrer.ToString();
    
  2. on view @Html.Hidden("RefUrl");
  3. on view @ Html.Hidden(“RefUrl”);
  4. on post Edit(int id,string RefUrl)

    在帖子编辑(int id,string RefUrl)

    return Redirect(RefUrl);
    

#1


10  

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

您可以使用Request.QueryString方法从URL获取一些值,用于示例:

@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.

在您的控制器中,将其作为参数字符串returnUrl接收。

#2


31  

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);

#3


10  

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

您可以从控制器中的Request获取Refer URL:

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.

这会将您重定向到调用URL。

#4


8  

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

在您的控制器类中使用Request.UrlReferrer。没有必要从页面传递URL。

   public ActionResult AddEntry(string ip, int TypeId)
    {

         // Do some stuff

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

#5


0  

@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

像这样更改你的代码

#6


-1  

  1. on Get like Edit(int? id)

    在Get上编辑(int?id)

    ViewBag.RefUrl = Request.UrlReferrer.ToString();
    
  2. on view @Html.Hidden("RefUrl");
  3. on view @ Html.Hidden(“RefUrl”);
  4. on post Edit(int id,string RefUrl)

    在帖子编辑(int id,string RefUrl)

    return Redirect(RefUrl);