使用路由概念将值从页面传递到页面 [英] passing a value from page to page using routing concept

查看:67
本文介绍了使用路由概念将值从页面传递到页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我有一个global.asax类文件,我在这里放置seo url路由概念:



 私人 静态  void  RegisterRoutes(RouteCollection路由)
{
// 有关url routing的更多信息http://www.codeproject.com/Articles/77199/URL-Routing-with-ASP-NET
routes.MapPageRoute( 〜/ Default.aspx);
routes.MapPageRoute( {categorySEOName} 〜/ Pages.aspx);
routes.MapPageRoute( {categorySEOName} / {subcategorySEOName} 〜/ Pages.aspx);
}





以及我如何从数据库获取数据并重写我的网址:



 public void LoadMenu()
{
CategoryInfo _CateogyInfo = new CategoryInfo();
SubCategoryInfo _SubcateoryInfo = new SubCategoryInfo();
列表< CategoryInfo > categoryList = _CateogyInfo.GetCategory()。ToList < CategoryInfo > ();
StringBuilder sb = new StringBuilder();
string appname = Request.ApplicationPath ==/? :Request.ApplicationPath.ToString();
sb.Append(< ul id =' nav' > );
foreach(categoryList中的CategoryInfo类别)
{
string seourl = string.Format({0} / {1},appname,category.CategorySEOName);
sb.Append(< li > < a class =' hsubs' href = + seourl + > + category.CategoryName +< / a > );
_SubcateoryInfo.CategoryId = category.CategoryId!= null? category.CategoryId:0;
_SubcateoryInfo.CategoryId = Convert.ToInt32(Request [categoryId]);
列表< SubCategoryInfo > SubcategoryList = _SubcateoryInfo.GetSubCategoryByCategoryId()。ToList < SubCategoryInfo > ();
if(SubcategoryList.Count> 0)
{
sb.Append(< ul class =' subs' > );
foreach(SubcategoryList中的SubCategoryInfo子类别)
{
seourl = string.Format({0} / {1} / {2},appname,category.CategorySEOName,subCategory.SubCategorySEOName) ;
sb.Append(< li > < a href = + seourl + > + subCategory.SubCategoryName +< / a > < / li > );
}
sb.Append(< / ul > );
}

}
sb.Append(< / ul > );
MenuSubMenu.Text = sb.ToString();
}







现在我的kestion是如何获得用户请求的categoryId将页面传递给页面。

就像用户点击类别一样,我想获得该类别,并将其作为请求[CategoryId]传递到我的下一页。



这是我用来获取请求ID的方式:



 app.Context.RewritePath (〜/ Category.aspx?Id =+ url [url.Length  -  2] +& name =+ url [url.Length  -  1] .Replace(。aspx,)+& ; type = category); 





现在如果我想获得请求的Id,我会将它传递给这样的变量:< br $>


 _ SubCategoryInfo.CategoryId = Convert.ToInt32(请求[  Id]); 





但是你知道这不是seo友好的。

现在我想使用相同的想法来使用路由概念获取所请求的Id。

解决方案

Hi,

I have a global.asax class file where I am placing the seo url routing concept like this:

private static void RegisterRoutes(RouteCollection routes)
    {
        //More info about url routing http://www.codeproject.com/Articles/77199/URL-Routing-with-ASP-NET
        routes.MapPageRoute("", "", "~/Default.aspx");
        routes.MapPageRoute("", "{categorySEOName}", "~/Pages.aspx");
        routes.MapPageRoute("", "{categorySEOName}/{subcategorySEOName}", "~/Pages.aspx");
    }



and this how I am getting the data from database and rewrite my url:

public void LoadMenu()
   {
       CategoryInfo _CateogyInfo = new CategoryInfo();
       SubCategoryInfo _SubcateoryInfo = new SubCategoryInfo();
       List<CategoryInfo> categoryList = _CateogyInfo.GetCategory().ToList<CategoryInfo>();
       StringBuilder sb = new StringBuilder();
       string appname = Request.ApplicationPath == "/" ? "" : Request.ApplicationPath.ToString();
       sb.Append("<ul id='nav'>");
       foreach (CategoryInfo category in categoryList)
       {
           string seourl = string.Format("{0}/{1}", appname, category.CategorySEOName);
           sb.Append("<li><a class='hsubs' href=" + seourl + ">" + category.CategoryName + "</a>");
           _SubcateoryInfo.CategoryId = category.CategoryId != null ? category.CategoryId : 0;
           _SubcateoryInfo.CategoryId = Convert.ToInt32(Request["categoryId"]);
           List<SubCategoryInfo> SubcategoryList = _SubcateoryInfo.GetSubCategoryByCategoryId().ToList<SubCategoryInfo>();
           if (SubcategoryList.Count > 0)
           {
               sb.Append("<ul class='subs'>");
               foreach (SubCategoryInfo subCategory in SubcategoryList)
               {
                   seourl = string.Format("{0}/{1}/{2}", appname, category.CategorySEOName, subCategory.SubCategorySEOName);
                   sb.Append("<li><a href=" + seourl + ">" + subCategory.SubCategoryName + "</a></li>");
               }
               sb.Append("</ul>");
           }

       }
       sb.Append("</ul>");
       MenuSubMenu.Text = sb.ToString();
  }




Now my kestion is how can I get a categoryId requested by the user to pass it from page to page.
Like once a user click on category I want to get that categoryId and pass it as the request["CategoryId"] to my next page.

This is the way I used to get the requested Id :

app.Context.RewritePath("~/Category.aspx?Id=" + url[url.Length - 2] + "&name=" + url[url.Length - 1].Replace(".aspx", "") + "&type=category");



Now If I want to get the Id requested I will pass it to a variable like this:

_SubCategoryInfo.CategoryId = Convert.ToInt32(Request["Id"]);



But as u know this is not seo friendly.
Now I want to use the same idea to get the requested Id using routing concept.

解决方案

这篇关于使用路由概念将值从页面传递到页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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