我正在努力解决有关 4.0 版本中的 asp.net webform 路由的两个问题。
第一个问题是本地服务器上的路径相同的路径适用于其他项目并在本地服务器中创建路径问题。
第二个问题是路由在实际托管 Web 服务器上不起作用
示例代码
全球.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;
}
来自buy-sell-in-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 页面上的 Current 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 页面 & 路由工作正常 & 当我点击它转到页面buy-sell-in-kashmir.aspx
http://localhost:59030/TravelKashir-Souq/buysell/1ef4f9db-14d1-4ff0-be79-922ce903bff4/buysell/1ef4f9db-14d1-4ff0-be79-922ce903bff4/electronics-technology
尝试了不同的路径仍然无法正常工作,当我在共享主机上上传代码时它不起作用并产生错误
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的网站和不起作用的路由版本http://demo2.kashmirsouq.com
配置:本地主机和共享主机上的 IIS 7.5/asp.net 4.0