这个 ASP.NET MVC ActionMethod 应该有两个视图吗?
我正在尝试使用 ASP.NET MVC3,并且有一个非常简单的网站,仅允许您登录/创建帐户或列出您的帐户详细信息。
我想做主页..基本上就是这样。如果您尚未登录,请要求他们登录或创建一个新帐户。否则,请向他们展示他们的帐户详细信息。简单的。
所以我不确定我是否应该做这样的事情......
public ActionResult Index()
{
return User.Identity.IsAuthenticated ? View("Show") : View("Index");
}
这是一个非常人为的例子,但它基本上描述了我的问题。
我觉得这不是一个好方法..但我应该重定向到不同的方法..就像真正的表演一样? ..例如。 /显示/UserFoo ????
我只是不确定人们在做什么——这是一个常见的模式。
想法?
I'm playing around with using ASP.NET MVC3 and have a very simple site that only allows you to log in/create an account OR list your account details.
I was wanting to make the homepage .. bascially that. If u're not logged in, then ask them to log in or create a new account. Otherwise, show them their account details. Simple.
So I wasn't sure if I should do something like this ....
public ActionResult Index()
{
return User.Identity.IsAuthenticated ? View("Show") : View("Index");
}
That's a pretty contrived example, but it basically describes my question.
I feel that this is not a good way.. but instead I should be redirecting to a different method .. like the real Show ? .. eg. /Show/UserFoo ????
I'm just not sure of what people are doing - a common pattern, here.
Thoughts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用
[Authorize]
属性检查用户是否经过身份验证,如果没有则重定向到登录页面。如果您不喜欢默认行为,您还可以编写自定义授权属性。
You could use the
[Authorize]
attribute which check if the user is authenticated and if not redirect to the login page.You could also write a custom authorization attribute if you don't like the default behavior.
我想说,不。主要是因为如果您希望两者之间有共同的内容,这将有助于您保持干燥。
相反,请在视图内检查某人是否经过身份验证。
Razor(干燥代码):
我认为这将有助于保持干燥。
I'd say, no. Mainly because this will help keep you DRY in case you want to have common content between the two.
Instead do checks inside the view for whether or not someone is authenticated.
Razor (Dry code):
I think this will help keep things DRY.
利用 LoginView 项:
您将需要一些东西来管理身份验证,
[Authorize]
属性可以为您做到这一点,或者您可以编写自己的自定义实现。Make use of the LoginView item:
You will need something to manage authentication, the
[Authorize]
attribute can do that for you or you can write your own custom implementation.