根据Web.config Razor发送到不同的页面 [英] Sending to different Pages depending on Web.config Razor

查看:58
本文介绍了根据Web.config Razor发送到不同的页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我想有一个状态页面,该页面将显示设备的不同布局,具体取决于谁在使用它,这将是web.config中定义的变量.我应该为每个视图创建一个单独的控制器吗?一些后台功能应该相似,但将来可能会有所不同.还是我应该拥有相同的cshtml文件,并根据是否使用它来隐藏html标记?

So I want to have a status page that will show a different layout of the equipment depending on who's using it which will be a variable defined in the web.config. Should I be creating a separate controller per view? Some of the background functions should be similar but some will probably be different in the future. Or should I have the same cshtml file and hide html markup depending on who's using it or not?

我正在考虑做类似的事情:

I was thinking of doing something like:

@if(System.Configuration.ConfigurationManager.AppSettings["IsSuperUser"] == "true")
{
    <a href="@Url.Action("SuperUserIndex", "Status")">Status</a>
}
else {
    <a href="@Url.Action("UserIndex", "Status")">Status</a>
}

这是个坏主意吗?

推荐答案

有几种选择,这完全取决于您的需求和偏好.

There are several options, it all depends on your needs and preferences.

您的代码可以使用,但是,您还必须仔细检查控制器中的权限!例如,您的网址将是"/SuperUser/Status"和"/User/Status".现在,是什么阻止了非超级用户在地址栏中输入"/SuperUser/Status"?

Your code will work, however you must also double check permission in your controller! For example, your url will be "/SuperUser/Status" and "/User/Status". Now, what's stopping non-super user to type in "/SuperUser/Status" to the address bar?

一个重要规则,永远不要信任最终用户!假定他们不会按照您的意愿去做.

One important rule, never trust the end users! Assume that they will not do what you intend them to do.

总而言之,我的偏好是在模型中包含一个变量以标识用户级别(超级与非超级),然后使用该变量来确定视图中的布局.请记住,您还可以基于变量/表达式来更改视图的布局.

Given all, my preference would be to include a variable in your Model to identify the user level (super vs non super), then use that to determine the layout in your views. Remember, you can also change the layout of the view based on variable/expression.

@Layout = Model.IsSuperUser ? "_SuperLayout.cshtml" : "_RegularLayout.cshtml";

这篇关于根据Web.config Razor发送到不同的页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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