URL路由中的.aspx [英] URL Routing in .aspx

查看:152
本文介绍了URL路由中的.aspx的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个房地产网站,并使用URL路由,所以我的客户可以有自己的精选房产展示自己在URL地址:www.realestatewebsite.com/featured/123-Fake-St

不过,我是新来的URL路径选择,我想我花了可怜的快捷方式。我会原本喜欢用我的数据库的主键来访问属性(PROPID),但并不希望有它传递到URL,这样反而使用的地址,以获得PROPID,并从那里使用它。但是,如果我尝试给Page.RouteData.Values​​ [地址]为字符串,并没有在URL地址没有它抛出一个异常。因此,我有一个try / catch语句来处理它。这似乎是不良的做法给我虽然。如果有人能告诉我,这是否是可以接受的事,或者如果有一个更好的解决方案,请让我知道。

这里是在Global.asax:

 无效的Application_Start(对象发件人,EventArgs的发送)
{
    //上的应用程序启动时运行code
    的RegisterRoutes(RouteTable.Routes);
}公共静态无效的RegisterRoutes(RouteCollection路线)
{
    routes.MapPageRoute(,精选/ {}地址,〜/精选/ Default.aspx的);
}

下面是从www.website/com/Featured/Default.aspx方法:

 保护INT getPropID()
{
    字符串的地址;
    INT PROPID = -1; //如果该方法返回-1页面只是列出全部精选房产    尝试
    {
        地址= Page.RouteData.Values​​ [地址]的ToString()。
    }
    赶上(异常前)
    {
        返回PROPID;
    }    如果(地址!= NULL)
    {
        字符串STRSQL =SELECT FROM PROPID WHERE tblFeatured地址='+地址+';
        DataTable的DT = da.FillDataTable(STRSQL);
        如果(dt.Rows.Count大于0)
            PROPID = Convert.ToInt32(dt.Rows [0] [PROPID]);
        返回PROPID;
    }
    其他
        返回PROPID;
}


解决方案

您正在以通常的方式接近这一点。 <一href=\"http://weblogs.asp.net/scottgu/archive/2009/10/13/url-routing-with-asp-net-4-web-forms-vs-2010-and-net-4-0-series.aspx\"相对=nofollow>斯科特谷的URL与ASP.NET 4 路由显示您正在使用完全相同的模式。


  • 设置的路线

  • 确保令牌的URL某处

  • 你的目标的页面加载
  • .aspx页面中,抢令牌,查询数据库。

  • 根据需要该令牌显示您的标记

如果您正在寻找的建议:


  • 每个属性应该有一个'塞'或数据库的令牌pre-提取。只是另一个属性来查询,而不是数字标识符。该数据表是没有帮助太多。

  • 保持你的令牌作为街道地址(也许包括城市SEO汁,如果你的客户不介意)

  • 使用该令牌去查查房子/属性记录在数据库中。在这一点忘掉数字ID。选择其中token匹配的属性,并将其写入到页面。逆足差为零。

 字符串propertyToken = Page.RouteData.Values​​ [地址]的ToString()。属性P = PropertyDatabase.GetSingleByToken(propertyToken);如果(P!= NULL)
{
    WritePropertyToPage(P);
}
其他
{
    //写页令牌未找到/拼写错误/过期/等。
}

I'm working on a real estate website and using URL Routing so my client can have their featured properties show their address in the URL: www.realestatewebsite.com/featured/123-Fake-St

However, I'm new to URL Routing and I think I took a poor shortcut. I would have originally liked to use the primary key of my database to access the properties (propID) but didn't want to have to pass it into the URL, so instead used the address to get the propID and use it from there. However, if I attempt to assign Page.RouteData.Values["address"] to a string and there is no address in the URL it throws an exception. Therefore I have a try/catch statement to handle it. This seems like poor practice to me though. If anyone can tell me whether or not this is acceptable to do, or if there's a much better solution, please let me know.

Here's the Global.asax:

void Application_Start(object sender, EventArgs e) 
{
    // Code that runs on application startup
    RegisterRoutes(RouteTable.Routes);
}

public static void RegisterRoutes(RouteCollection routes)
{
    routes.MapPageRoute("", "Featured/{address}", "~/Featured/Default.aspx");
}

And here's a method from www.website/com/Featured/Default.aspx:

protected int getPropID()
{
    string address;
    int propID = -1; //If the method returns -1 the page just lists all featured properties

    try
    {
        address = Page.RouteData.Values["address"].ToString();
    }
    catch (Exception ex)
    {
        return propID;
    }

    if (address != null)
    {
        string strSQL = "SELECT propID FROM tblFeatured WHERE address = '" + address + "'";
        DataTable dt = da.FillDataTable(strSQL);
        if (dt.Rows.Count > 0)
            propID = Convert.ToInt32(dt.Rows[0]["propID"]);
        return propID;
    }
    else
        return propID;
}

解决方案

You're approaching this in the typical manner. Scott Gu's URL Routing with ASP.NET 4 shows the exact same pattern you're using.

  • setup the route
  • ensure a token is on the URL somewhere
  • on page load of your target .aspx page, grab the token, query the db.
  • display your markup as needed for that token

If you were looking for suggestions:

  • Each property should have a 'slug' or token pre-extracted in your database. Just another property to query on, not the numeric identifier. The DataTable isn't helping too much.
  • Keep your token as the street address (perhaps include city for SEO juice if your client doesn't mind)
  • Use that token to go look up the house/property record in the database. Forget numeric IDs at this point. SELECT the property where token matches, and write it to the page. Perf difference is zero.

string propertyToken =  Page.RouteData.Values["address"].ToString();

Property p = PropertyDatabase.GetSingleByToken(propertyToken);

if(p!=null)
{
    WritePropertyToPage(p);
}
else
{
    //write to page that token wasn't found/misspelled/expired/etc.
}

这篇关于URL路由中的.aspx的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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