在ASP。NET MVC,如何响应。重定向的工作吗?

时间:2021-09-04 17:40:18

I have used response.redirect in classic ASP and ASP.NET webforms. However, with MVC 2.0, I am running into something peculiar.

我用的反应。在经典ASP和ASP中重定向。净webforms。然而,在MVC 2.0中,我遇到了一些奇怪的事情。

I have a private method in a controller class that is used by multiple controller methods to help load and validate some information. This private method is setup to redirect if a problem is discovered to a generic error message page.

我在控制器类中有一个私有方法,它被多个控制器方法用于帮助加载和验证一些信息。如果问题被发现到通用错误消息页面,则设置此私有方法进行重定向。

The big problem I am noticing is that the calling controller class and page view attempt to complete rendering and loading before the redirect actually takes place. This is annoying in development because the View throws exceptions that I need to ignore before my generic error page finally loads.

我注意到的一个大问题是,调用控制器类和页面视图试图在重定向发生之前完成呈现和加载。这在开发中很烦人,因为视图抛出了一些异常,我需要在常规错误页面最终加载之前忽略这些异常。

As mentioned above, I am used to the older model of response.redirect which prevented subsequent code on a page from being executed as the new page would then load.

如上所述,我已经习惯了旧的响应模型。重定向,阻止页面上的后续代码在新页面加载时执行。

Any help or advice on redirects in MVC would be greatly appreciated.

如果您对MVC中的重定向有任何帮助或建议,我们将不胜感激。

5 个解决方案

#1


43  

The conventional mechanism to redirect in ASP.Net MVC is to return an object of type RedirectResult to the client. If this is done before your View method is called, your view methods will never be called.

常规的ASP重定向机制。Net MVC是将RedirectResult类型的对象返回给客户端。如果在调用视图方法之前执行了此操作,则永远不会调用视图方法。

If you call Response.Redirect yourself, instead of letting Asp.Net MVC's front controller do that for you, your controller method will continue on until it finishes or throws an exception.

如果你调用的响应。重定向自己,而不是让Asp。Net MVC的前端控制器为您做这个,您的控制器方法将继续,直到它完成或抛出一个异常。

The idiomatic solution for your problem is to have your private method return a result that your controller can use.

针对您的问题的惯用解决方案是让您的私有方法返回一个您的控制器可以使用的结果。

for example:

例如:

public ActionResult Edit(MyEntity entity)
{
  if (!IsValid()) return Redirect("/oops/");
  ...
  return View();

}

private bool IsValid()
{
  if (nozzle==NozzleState.Closed) return false;
  if (!hoseAttached) return false;
  return (userRole==Role.Gardener);
}

#2


19  

In ASP.NET MVC, you would normally redirect to another page by returning a RedirectResult from the controller method.

在ASP。NET MVC,通过从controller方法返回RedirectResult,您通常会重定向到另一个页面。

Example:

例子:

public ActionResult Details(int id)
{
     // Attempt to get record from database
     var details = dataContext.GetDetails(id);

     // Does requested record exist?
     if (details == null)
     {
         // Does not exist - display index so user can choose
         return RedirectToAction("Index");
     }

     // Display details as usual
     return View(details);
}

#3


8  

In MVC never use Response.Redirect use

在MVC中从不使用响应。使用重定向

return RedirectToAction("ActionResultName", "ControllerName");

As to WHY to NOT use Response.Redirect in MVC:

至于为什么不使用响应。在MVC重定向:

  1. Performance issues with it
  2. 与它的性能问题
  3. Not following standard asp.net MVC pattern and conventions that have been built specifically FOR MVC.
  4. 不遵循标准的asp.net MVC模式和专门为MVC构建的约定。

#4


1  

its recommended and its the only way to redirect to asp.net form in mvc controller action by using

它是推荐的,也是唯一通过使用mvc控制器操作重定向到asp.net表单的方法

return Redirect("/page/pagename.aspx");

other way we can redirect by using ( not recommended and bad way )

我们可以使用(不推荐或不正确的方式)重定向

Response.Redirect("/page/pagename.aspx", true);

it will work for redirect, but problem is it will clear all of our Session values. so why its not recommended.

它将用于重定向,但问题是它将清除所有会话值。为什么不推荐呢?

#5


-3  

try this code in mvc view page lode

在mvc视图页面lode中尝试此代码。

        if (Session["UserName"] == null)
        {
            this.Response.Redirect("LogOn");

        }

#1


43  

The conventional mechanism to redirect in ASP.Net MVC is to return an object of type RedirectResult to the client. If this is done before your View method is called, your view methods will never be called.

常规的ASP重定向机制。Net MVC是将RedirectResult类型的对象返回给客户端。如果在调用视图方法之前执行了此操作,则永远不会调用视图方法。

If you call Response.Redirect yourself, instead of letting Asp.Net MVC's front controller do that for you, your controller method will continue on until it finishes or throws an exception.

如果你调用的响应。重定向自己,而不是让Asp。Net MVC的前端控制器为您做这个,您的控制器方法将继续,直到它完成或抛出一个异常。

The idiomatic solution for your problem is to have your private method return a result that your controller can use.

针对您的问题的惯用解决方案是让您的私有方法返回一个您的控制器可以使用的结果。

for example:

例如:

public ActionResult Edit(MyEntity entity)
{
  if (!IsValid()) return Redirect("/oops/");
  ...
  return View();

}

private bool IsValid()
{
  if (nozzle==NozzleState.Closed) return false;
  if (!hoseAttached) return false;
  return (userRole==Role.Gardener);
}

#2


19  

In ASP.NET MVC, you would normally redirect to another page by returning a RedirectResult from the controller method.

在ASP。NET MVC,通过从controller方法返回RedirectResult,您通常会重定向到另一个页面。

Example:

例子:

public ActionResult Details(int id)
{
     // Attempt to get record from database
     var details = dataContext.GetDetails(id);

     // Does requested record exist?
     if (details == null)
     {
         // Does not exist - display index so user can choose
         return RedirectToAction("Index");
     }

     // Display details as usual
     return View(details);
}

#3


8  

In MVC never use Response.Redirect use

在MVC中从不使用响应。使用重定向

return RedirectToAction("ActionResultName", "ControllerName");

As to WHY to NOT use Response.Redirect in MVC:

至于为什么不使用响应。在MVC重定向:

  1. Performance issues with it
  2. 与它的性能问题
  3. Not following standard asp.net MVC pattern and conventions that have been built specifically FOR MVC.
  4. 不遵循标准的asp.net MVC模式和专门为MVC构建的约定。

#4


1  

its recommended and its the only way to redirect to asp.net form in mvc controller action by using

它是推荐的,也是唯一通过使用mvc控制器操作重定向到asp.net表单的方法

return Redirect("/page/pagename.aspx");

other way we can redirect by using ( not recommended and bad way )

我们可以使用(不推荐或不正确的方式)重定向

Response.Redirect("/page/pagename.aspx", true);

it will work for redirect, but problem is it will clear all of our Session values. so why its not recommended.

它将用于重定向,但问题是它将清除所有会话值。为什么不推荐呢?

#5


-3  

try this code in mvc view page lode

在mvc视图页面lode中尝试此代码。

        if (Session["UserName"] == null)
        {
            this.Response.Redirect("LogOn");

        }