我做了自定義的HttpModule,它搜索$ where param,更改它(用DateTime.UtcNow替換DateTime.Now)並重寫路徑。簡化的模塊實現的樣子:重寫WCF RIA Services請求URL
private void OnBeginRequest(object sender, EventArgs e)
{
HttpApplication app = sender as HttpApplication;
string whereQuery = app.Request.QueryString["$where"];
string newWhereQuery = this.ChangeWhereQuery(whereQuery);
// Combine new where query string with other query strings
string newQueryString = this.BuildQueryString(app, newWhereQuery);
app.Context.RewritePath(app.Request.FilePath, app.Request.PathInfo, newQueryString);
}
問題是,重寫查詢表達式後丟失,即在的DomainService的查詢方法queryDescription.Query爲null:
public override IEnumerable Query(QueryDescription queryDescription, out IEnumerable<ValidationResult> validationErrors, out int totalCount)
{
// queryDescription.Query is null
return base.Query(queryDescription, out validationErrors, out totalCount);
}
如果我用下面的代碼替換我的自定義模塊(路徑改寫爲原始查詢字符串),一切工作正常:
private void OnBeginRequest(object sender, EventArgs e)
{
HttpApplication app = sender as HttpApplication;
string qs = app.Request.QueryString.ToString();
app.Context.RewritePath(app.Request.FilePath, app.Request.PathInfo, qs);
}
我發現Silverlight的論壇上這個帖子,但螺紋網址不再可用:
WCF RIA Services本身使用URL重寫,因此可能會產生衝突。 這裏是最近線程了一個解決方案: http://forums.silverlight.net/forums/p/233310/573340.aspx#573340
爲什麼查詢表達式改變URL查詢字符串後失去了任何想法?
我使用WCF RIA服務V1.0 SP2,.NET 4和IIS 7
在發送到base.Query之前更改QueryDescriptor也是想法,但重寫路徑看起來更簡單。現在我使用ExpressionVisitor替換QueryDescription.Query.Expression中的DateTime.Now表達式,它工作正常。謝謝! – Davor