最後使用免費的套件來ISAPIRewrite處理
(如有其他需求可參考這篇Tip/Trick: Url Rewriting with ASP.NET )
不過順利解決完rewrite的問題後。
又遇到二個問題需要處理lol~不過解法都在找到的資料裡面,
以下只是記錄一下流程跟心得:)
問題一:如何處理rewrite後的postback問題
Step1:先新增一個class:
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
// Original code here:
//http://weblogs.asp.net/scottgu/archive/2007/02/26/tip-trick-url-rewriting-with-asp-net.aspx
public class FormRewriterControlAdapter : System.Web.UI.Adapters.ControlAdapter
{
protected override void Render(HtmlTextWriter writer)
{
base.Render(new RewriteFormHtmlTextWriter(writer));
}
}
public class RewriteFormHtmlTextWriter : HtmlTextWriter
{
public RewriteFormHtmlTextWriter(HtmlTextWriter writer)
: base(writer)
{
this.InnerWriter = writer.InnerWriter;
}
public RewriteFormHtmlTextWriter(System.IO.TextWriter writer)
: base(writer)
{
base.InnerWriter = writer;
}
public override void WriteAttribute(string name, string value, bool fEncode)
{
// If the attribute we are writing is the "action" attribute, and we are not on a sub-control,
// then replace the value to write with the raw URL of the request - which ensures that we'll
// preserve the PathInfo value on postback scenarios
if (name == "action")
{
HttpContext context = HttpContext.Current;
if (context.Items["ActionAlreadyWritten"] == null)
{
// Because we are using an url reweriting HttpModule, we will use the
// Request.RawUrl property within ASP.NET to retrieve the origional URL
// before it was re-written. You'll want to change the line of code below
// if you use a different URL rewriting implementation.
// value = context.Request.RawUrl;
value = context.Request.ServerVariables["HTTP_X_REWRITE_URL"];
// Indicate that we've already rewritten the
FormRewriterControlAdapter.cs,並將它放到App_Code資料夾Step2:新增Asp.net的App_Browsers
Step3:於App_Browsers內新增Form.browser,並新增以下內容
<browsers>
<browser refID="Default">
<controlAdapters>
<adapter controlType="System.Web.UI.HtmlControls.HtmlForm"
adapterType="FormRewriterControlAdapter" />
</controlAdapters>
</browser>
</browsers>
<img id="userAvatar" runat="server" src="~/images/pic.jpg" width="50" visible="false">
另外我的javascript的include方式,因為不是用asp.net元件(沒試著加runat="server"),所以就從/開始指到資源檔就正確了
<script type="text/javascript" src="/js/plugin/jquery.copy.js"></script>)
Reference:
URL Rewriting for ASP.NET Web Forms
Tip/Trick: Url Rewriting with ASP.NET
Handling ASP.NET PostBacks with URL RewritingA Complete URL Rewriting Solution for ASP.NET 2.0
ASP.NET URL Rewrite. URL重写
沒有留言:
張貼留言
留個話吧:)