ajr*_*rpc 3 c# asp.net design-patterns coding-style
我有一个带有一些id的表,我想在default.aspx中打开一个带有表单的某个page.aspx,具体取决于id.
我现在拥有的是:
if(id_table ==1) {
response.redirect("PageBla.aspx");
}
if(id_table==2) {
response.redirect("Page21231.aspx");
}
if(id_table==6) {
....
}
etc etc....
Run Code Online (Sandbox Code Playgroud)
如果我有少量的ID要检查,这很简单.但我会检查几十个ID.有没有编程模式或任何其他方法这样做没有几十个ifs或开关/案例?
提前致谢
编辑:"="被替换为"==".
只需创建一个简单的URL数组,如下所示:
string[] urls = {"PageBla.aspx", "Page21231.aspx"};
response.redirect(urls[id_table]);
Run Code Online (Sandbox Code Playgroud)
如果您有一个更复杂的用例,另一个选择是使用约定优于配置.你可以这样做:
您的重定向代码将如下所示:
Response.Redirect(tableId + ".asxp");
Run Code Online (Sandbox Code Playgroud)包含ID和URL的查找很容易.它可以在数据库中提供灵活性,但您现在也可以将它们放入字典中,如果您发现需要它,可以稍后添加数据库部分.
您可以将查找声明为字段:
private static readonly Dictionary<int, string> redirectLookup = new Dictionary<int,string> {
{1, "PageBla.aspx"},
{2, "Page21231.aspx"},
// .....
{6, "somepage6.apx"}
};
Run Code Online (Sandbox Code Playgroud)
在您的重定向逻辑中:
string redirect;
if (redirectLookup.TryGetValue(id_table, out redirect))
Response.Redirect(redirect);
else
// some default action when that ID was not mapped.
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
244 次 |
最近记录: |