ASP.Net 4.0路径问题与路径

时间:2012-12-04 08:48:06

标签: c# webforms asp.net-routing

我正在努力解决有关4.0版本的asp.net webform路由的两个问题。

第一个问题与本地服务器相同路径上的路径适用于其他项目&在本地服务器中创建路径问题。

第二个问题是路由在实际托管Web服务器上不起作用

示例代码

Global.asx

public static void RegisterRoutes(RouteCollection routes)
{
    routes.Ignore("{*allaspx}", new { allaspx = @".*\.aspx(/.*)?" });
    routes.Ignore("{*allcss}", new { allcss = @".*\.css(/.*)?" });
    routes.Ignore("{*alljpg}", new { alljpg = @".*\.jpg(/.*)?" });
    routes.Ignore("{*alljpg}", new { alljpg = @".*\.png(/.*)?" });
    routes.Ignore("{*alljpg}", new { alljpg = @".*\.gif(/.*)?" });
    routes.Ignore("{*alljs}", new { alljs = @".*\.js(/.*)?" });
    routes.Add(new System.Web.Routing.Route("{resource}.css/{*pathInfo}", new System.Web.Routing.StopRoutingHandler()));
    routes.Add(new System.Web.Routing.Route("{resource}.js/{*pathInfo}", new System.Web.Routing.StopRoutingHandler()));

    // Page will Generate error if route for home page is take out then 
    // http://www.kashmirsouq.com without any page or quesry string wont work.

    routes.MapPageRoute(
         "HomeRoute",
         "",
         "~/Default.aspx"
     );


    ////For 
    routes.MapPageRoute("ParentCat", "buysell/{CatID}/{Title}", "~/buy-sell-in-kashmir.aspx", false,
         new RouteValueDictionary {
                    { "CatID", "0" },
                    { "Title", "Product-Category-not-found" }},
         new RouteValueDictionary {   
                    { "CatID", "^[0-9a-fA-Z-]{36}$" }
                }); 
}

ParentCategoryListing.ascx& .cs文件代码

            <asp:Repeater ID="rptParentCategoryListing" runat="server">
                <ItemTemplate>
                    <li><a href='<%#getURLRouting(Eval("parentGUID"),Eval("CatName")) %>' title='<%#  Eval("CatLinkTitle")%>'>
                        <%#  Eval("CatName")%></a> </li>
                </ItemTemplate>
            </asp:Repeater>




protected string getURLRouting(object CatID, object CatTitle)
{
    string strTitle = CatTitle.ToString();

    #region Generate SEO Friendly URL based on Title
    //Trim Start and End Spaces.
    strTitle = strTitle.Trim();
    //Trim "-" Hyphen
    strTitle = strTitle.Trim('-');
    strTitle = strTitle.ToLower();
    char[] chars = @"$%#@!*?;:~`+=()[]{}|\'<>,/^&"".".ToCharArray();

    //Replace . with - hyphen
    strTitle = strTitle.Replace(".", "-");
    //Replace Special-Characters
    for (int i = 0; i < chars.Length; i++)
    {
        string strChar = chars.GetValue(i).ToString();
        if (strTitle.Contains(strChar))
        {
            strTitle = strTitle.Replace(strChar, string.Empty);
        }
    }
    //Replace all spaces with one "-" hyphen
    strTitle = strTitle.Replace(" ", "-");
    //Replace multiple "-" hyphen with single "-" hyphen.
    strTitle = strTitle.Replace("--", "-");
    strTitle = strTitle.Replace("---", "-");
    strTitle = strTitle.Replace("----", "-");
    strTitle = strTitle.Replace("-----", "-");
    strTitle = strTitle.Replace("----", "-");
    strTitle = strTitle.Replace("---", "-");
    strTitle = strTitle.Replace("--", "-");
    //Run the code again...
    //Trim Start and End Spaces.
    strTitle = strTitle.Trim();
    //Trim "-" Hyphen
    strTitle = strTitle.Trim('-');
    #endregion
    string url = null;
    try
    {
       // url = "~/news/" + NewsID + "/" + pageid + "/" + strTitle; // +"&pgName=" + PageName;
        url = "~/buysell/"+CatID + "/" + strTitle;
     //   return url;
    }
    catch (Exception ex)
    {
        Response.Redirect("Error");
    }
    return url;
}

购买 - 出售在kashmir.aspx文件中的部分代码

protected void Page_Load(object sender, EventArgs e)
{
    if (string.IsNullOrEmpty(Request["CatID"]))
    {
        sPID = RouteData.Values["CatID"].ToString();
        bIsGUID = IsGuid(sPID, out PID);
        addCanonicalURL = false;
    }
    else
    {
        sPID = Helper.GetQueryStringValue("CatID").ToString(); ;
        bIsGUID = IsGuid(sPID, out PID);
        addCanonicalURL = true;
    }


  // Code to get details based on sPID ParentCategory GUID

}

输出

使用default.aspx页面上的当前UserControl生成父类别的以下链接

http://localhost:59030/TravelKashir-Souq/~/buysell/1ef4f9db-14d1-4ff0-be79-922ce903bff4/electronics-technology

在上面的示例链接中,当我在用户控制函数~/中使用路径时,它会添加getURLRouting

url = "~/buysell/"+CatID + "/" + strTitle;

如果我使用路径

然后

url = "buysell/"+CatID + "/" + strTitle;将父类别更改链接到

http://localhost:59030/TravelKashir-Souq/buysell/1ef4f9db-14d1-4ff0-be79-922ce903bff4/electronics-technology

在default.aspx页面&amp;路由工作正常&amp;当我点击它进入页面买卖-kashmir.aspx

http://localhost:59030/TravelKashir-Souq/buysell/1ef4f9db-14d1-4ff0-be79-922ce903bff4/buysell/1ef4f9db-14d1-4ff0-be79-922ce903bff4/electronics-technology

on已经尝试了不同的路径仍然无法正常工作,当我在共享主机上传代码时它不起作用&amp;生成错误

HTTP Error 404.0 - Not Found The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.

我对此问题感到沮丧,因为我可以在代码中提到的相同路径适用于其他项目而没有任何问题。

我很感激这方面的帮助。

我们的路由http://www.kashmirSouq.com&amp;路由版本不起作用http://demo2.kashmirsouq.com

配置:localhost&amp;上的IIS 7.5 / asp.net 4.0共享主机

0 个答案:

没有答案