ASP.NET-避免硬编码路径

问题描述 投票:6回答:4

我正在寻找一种最佳实践的解决方案,旨在减少ASP.NET应用程序中硬编码的URL的数量。

例如,当查看产品详细信息屏幕,对这些详细信息进行编辑然后提交更改时,用户将被重定向回产品列表屏幕。而不是编码以下内容:

Response.Redirect("~/products/list.aspx?category=books");

我希望有一个可以让我做这样的事情的解决方案:

Pages.GotoProductList("books");

其中Pages是公共基类的成员。

我只是在这里吐痰,很想听听任何其他方式来管理人的应用程序重定向。

编辑

我最终创建了以下解决方案:我已经有了一个通用的基类,在其中添加了一个Pages枚举(感谢Mark),每个项目的System.ComponentModel.DescriptionAttribute属性都包含页面的URL:

public enum Pages
{
    [Description("~/secure/default.aspx")]
    Landing,
    [Description("~/secure/modelling/default.aspx")]
    ModellingHome,
    [Description("~/secure/reports/default.aspx")]
    ReportsHome,
    [Description("~/error.aspx")]
    Error
}

然后,我创建了一些重载方法来处理不同的情况。我使用反射通过它的Description属性获取页面的URL,然后将查询字符串参数作为匿名类型传递(也使用反射将每个属性添加为查询字符串参数):

private string GetEnumDescription(Enum value)
{
    Type type = value.GetType();
    string name = Enum.GetName(type, value);

    if (name != null)
    {
        FieldInfo field = type.GetField(name);
        if (field != null)
        {
            DescriptionAttribute attr = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute;

            if (attr != null)
                return attr.Description;
        }
    }

    return null;
}

protected string GetPageUrl(Enums.Pages target, object variables)
{
    var sb = new StringBuilder();
    sb.Append(UrlHelper.ResolveUrl(Helper.GetEnumDescription(target)));

    if (variables != null)
    {
        sb.Append("?");
        var properties = (variables.GetType()).GetProperties();

        foreach (var property in properties)
            sb.Append(string.Format("{0}={1}&", property.Name, property.GetValue(variables, null)));
    }

    return sb.ToString();
}

protected void GotoPage(Enums.Pages target, object variables, bool useTransfer)
{
    if(useTransfer)
        HttpContext.Current.Server.Transfer(GetPageUrl(target, variables));
    else
        HttpContext.Current.Response.Redirect(GetPageUrl(target, variables));
}

然后,一个典型的呼叫如下所示:

GotoPage(Enums.Pages.Landing, new {id = 12, category = "books"});

评论?

asp.net url response.redirect
4个回答
4
投票
public class MyPageClass : Page { private const string productListPagePath = "~/products/list.aspx?category="; protected void GotoProductList(string category) { Response.Redirect(productListPagePath + category); } }

然后,在后面的代码中,确保您的页面源自此类:

 public partial class Default : MyPageClass
 {
      ...
 }

在此范围内,您可以仅通过使用:]进行重定向

 GotoProductList("Books");

现在,这有点受限制,因为毫无疑问,您将拥有各种其他页面,例如ProductList页面。您

could
在页面类中为它们中的每一个赋予自己的方法,但这有点令人不快,并且无法顺利扩展。

我通过在数据库表中保留页面名称/文件名映射来解决类似的问题(我在调用外部的,动态添加的HTML文件,而不是ASPX文件,因此我的需求有所不同,但是我认为原则适用)。然后,您的呼叫将使用字符串或更好的枚举来重定向:

protected void GoToPage(PageTypeEnum pgType, string category) { //Get the enum-to-page mapping from a table or a dictionary object stored in the Application space on startup Response.Redirect(GetPageString(pgType) + category); // *something* like this }

从您的页面,您的呼叫将是:GoToPage(enumProductList,“ Books”);

很好的是,该调用是对祖先类中定义的函数的调用(无需传递或创建管理器对象),并且路径非常明显(如果使用枚举,智能将限制您的范围)。

祝你好运!


1
投票

1
投票

0
投票
© www.soinside.com 2019 - 2024. All rights reserved.