MVC-3 ASP.NET 共享视图-重定向-Razor

发布于 2024-11-10 14:52:34 字数 512 浏览 0 评论 0原文

我在“Views/Shared”文件夹中有一个名为 NotAuthorized 的共享视图。当用户无权查看该页面时,我想将用户重定向到此视图。

最初,该视图位于名为 Account 的文件夹中。但我将其移至共享文件夹中,因为我不再使用该帐户。我已经删除了帐户文件夹。

我使用以下代码进行重定向:

public ActionResult NotAuthorised()
{  
   return RedirectToAction("NotAuthorised", "Account");
}

现在我删除了“帐户”文件夹,我尝试使用

public ActionResult NotAuthorised()
{  
   return RedirectToAction("NotAuthorised", "Shared");
}

在最后一行中提供共享的文件夹名称,这是完全错误的。

谁能告诉我,我做错了什么?

谢谢

I have a shared view called NotAuthorised in the folder 'Views/Shared'. I want to redirect the users to this view when they are not authorised to see the page.

Initially, this view was in a folder called Account. But I moved it into the Shared folder as I am not using the Account anymore. I have deleted the Account folder.

I used the following code to redirect:

public ActionResult NotAuthorised()
{  
   return RedirectToAction("NotAuthorised", "Account");
}

Now that I removed the Account folder, I'm trying to use

public ActionResult NotAuthorised()
{  
   return RedirectToAction("NotAuthorised", "Shared");
}

I am completely wrong by giving the folder name shared in the last line.

Could anyone tell me, what I am doing wrong?

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

那些过往 2024-11-17 14:52:34

您无法重定向到View,只能重定向到ControllerAction。您必须为重定向指定一个控制器操作,然后您可以在其中呈现共享视图。

public class AuthorizeController : Controller
{
    public ActionResult NotAuthorised()
    {  
       return View("NotAuthorised");
    }
}

然后从任何其他操作方法中重定向到这个新操作:

return RedirectToAction("NotAuthorised", "Authorize");

但是您可能不需要这个额外的控制器。您可以简单地渲染共享的View

public ActionResult NotAuthorised()
{  
   return View("NotAuthorised");
}

You can't redirect to a View, only to an Action of a Controller. You have to specify an controller action for your redirect and there you can render your shared view.

public class AuthorizeController : Controller
{
    public ActionResult NotAuthorised()
    {  
       return View("NotAuthorised");
    }
}

and later redirect to this new action from within any other action method:

return RedirectToAction("NotAuthorised", "Authorize");

But you may not need this additional Controller. You could simply render the shared View

public ActionResult NotAuthorised()
{  
   return View("NotAuthorised");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文