Redirect from a view to another view

后端 未结 3 351
悲&欢浪女
悲&欢浪女 2020-12-16 10:02

Hi I am trying to redirect from a view to a different view but I get a red squigly in visual studio.How can I redirect from inside a view to another view.This is what I have

3条回答
  •  伪装坚强ぢ
    2020-12-16 10:26

    That's not how ASP.NET MVC is supposed to be used. You do not redirect from views. You redirect from the corresponding controller action:

    public ActionResult SomeAction()
    {
        ...
        return RedirectToAction("SomeAction", "SomeController");
    }
    

    Now since I see that in your example you are attempting to redirect to the LogOn action, you don't really need to do this redirect manually, but simply decorate the controller action that requires authentication with the [Authorize] attribute:

    [Authorize]
    public ActionResult SomeProtectedAction()
    {
        ...
    }
    

    Now when some anonymous user attempts to access this controller action, the Forms Authentication module will automatically intercept the request much before it hits the action and redirect the user to the LogOn action that you have specified in your web.config (loginUrl).

提交回复
热议问题