2012-07-19 55 views
0

什么是URL重写在.NET 4我正在寻找一个简单的解决方案,以重写URL像URL在.NET 4

“myurl.com/ApplicationName/Kohls”的东西是最好的解决方案重写“myurl.com/ApplicationName/index.html?store=Kohls”,所以我可以通过查询字符串访问var“Kohls”。

我目前使用Global.asax,它一直在为上述案件工作 - 但我遇到麻烦的情况下,用户将进入myurl.com/Application,没有“/”或任何事情后应用。

目前,我有这样的:

protected void Application_BeginRequest(object sender, EventArgs e) 
    { 
     String CurrentURLPath = Request.Path.ToUpper(); 

     Match nothingAfterRoot = Regex.Match(CurrentURLPath, @"/ApplicationName(/)?$", RegexOptions.IgnoreCase); 
     if (nothingAfterRoot.Success) 
     { 
      HttpContext myContext = HttpContext.Current; 
      myContext.RewritePath("/ApplicationName/Default.aspx?store=ABC"); 
     } 
     else 
     { 
      Match match = Regex.Match(CurrentURLPath, @"/ApplicationName(/)?(\w)*$", RegexOptions.IgnoreCase); 
      if (match.Success) 
      { 
       CurrentURLPath = CurrentURLPath.Trim('/'); 
       String store= CurrentURLPath.Split("ApplicationName/".ToCharArray())[1]; 
       HttpContext myContext = HttpContext.Current; 
       myContext.RewritePath(String.Format("/ApplicationName/Default.aspx?store={0}", store)); 
      } 
     } 
    } 

回答

2

什么是URL重写在.NET 4

使用MVC框架,如果你可以选择最佳的解决方案。 URL重写是默认的。

对于传统的asp.net网站,我用的是UrlRewritingNet.UrlRewriter.dll有以下我的web.config文件:

<configSections> 
    <section name="urlrewritingnet" restartOnExternalChanges="true" 
     requirePermission="false" type="UrlRewritingNet.Configuration.UrlRewriteSection, 
     UrlRewritingNet.UrlRewriter" /> 
</configSections> 

<system.web> 
    <urlrewritingnet rewriteOnlyVirtualUrls="true" contextItemsPrefix="QueryString" 
     defaultPage="default.aspx" defaultProvider="RegEx"  
     xmlns="http://www.urlrewriting.net/schemas/config/2006/07"> 
     <rewrites> 
      <add name="ArticleUrlRewrite" virtualUrl="^~/a-(.*)-([\w-]*)\.aspx" 
       rewriteUrlParameter="ExcludeFromClientQueryString" 
       destinationUrl="~/article.aspx?id=$1" ignoreCase="true" /> 
     </rewrites> 
    </urlrewritingnet> 
</system.web> 
1

可以使用SHORTURL,DOTNET重写一个URL像位。 LY/XXXXwww.mysite.com/index.htm

它改变默认的错误页面的重定向文件,并根据若该值存在,它会将您重定向到真实的URL。

如果你有兴趣在此解决方案,您可以找到更多的 http://sourceforge.net/projects/shorturl-dotnet/

using System; 
using System.Web; 

public partial class Redirection : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     ShortUrl.Container oShortUrl; 

     if (ShortUrl.Utils.HasValue(Request.QueryString["page"].ToString())) // checks in case ISAPIRewrite is being used 
     { 
      oShortUrl = ShortUrl.Utils.RetrieveUrlFromDatabase(Request.QueryString["page"].ToString()); 
     } 
     else // using IIS Custom Errors 
     { 
      oShortUrl = ShortUrl.Utils.RetrieveUrlFromDatabase(ShortUrl.Utils.InternalShortUrl(Request.Url.ToString())); 
     } 

     if (oShortUrl.RealUrl != null) 
     { 
      Response.Redirect(oShortUrl.RealUrl); 
     } 
     else 
     { 
      Response.Redirect("MissingUrl.aspx"); 
     } 
    } 
} 
0

如上所述,MVC是一个好方法,如果它是一个选项。

如果你正在运行IIS 7,有可用的URL重写模块:

http://learn.iis.net/page.aspx/460/using-the-url-rewrite-module/http://www.microsoft.com/en-us/download/details.aspx?id=7435

如果你正在运行IIS6,您可以使用urlrewritingnet如上所述,或如我喜欢,IIRF:

http://iirf.codeplex.com/

国防部重写的相似性和基于正则表达式的规则使它非常powerfu l