ASP.NET MVC的部分意见和重定向 [英] ASP.NET MVC partial views and redirecting

查看:79
本文介绍了ASP.NET MVC的部分意见和重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为Login.ascx的局部视图有我的登录框,我包括了一些在我的网站页面。当信息是正确的,但我想这样做验证,所以如果登录信息是不正确的,我想将用户重定向回他们在之前的观点向他们显示登录错误,它工作正常。什么是说的正确方法,返回你来自的看法?

I have a partial view called Login.ascx that has my login boxes that I include on a number of pages throughout my site. It works fine when the information is correct but I'm trying to do validation so if the login info is incorrect, I want to redirect the user back to the view they were in before to show them the login errors. What is the correct way of saying, return to the view you came from?

推荐答案

听起来像是而不是问我是如何做到这一点,你应该问自己,我为什么这样做。也许这是一个设计决策,而不是一个技术问题。

Sounds like instead of asking how I do this, you should be asking yourself WHY am I doing it this way. Maybe it's a design decision rather than a technical question.

但如果你真的要对多个登录页面,你可以尝试一个控制器的动作...

Though if you're really going to have one controller actions for multiple login pages you can try...

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

或在TempData的保持路由名称,只是用一个RedirectToRoute(TempData的[LoginRoute]);

Or keep the route name in TempData and just use a RedirectToRoute(TempData["LoginRoute"]);

这两种解决方案有一个坏code气味虽然。

Both solutions have a bad code smell though.

请注意,如果你不检查跨站点注射的只是要重提到其他站点。您可能需要做参照网址一些验证。

Note that if you're not checking for cross-site injections that is just going to refer back to the other site. You may want to do some validation on the referring URL.

这篇关于ASP.NET MVC的部分意见和重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆