我正在嘗試從ASP.NET MVC預覽版3更新項目到預覽版5,似乎Controller.ReadFromRequest(字符串鍵)已從控制器類中刪除。有誰知道基於表單中的標識符檢索信息的替代方法嗎?什麼是ASP.NET MVC中Controller.ReadFromRequest的替代?
5
A
回答
3
看起來他們已經添加controller.UpdateModel來解決這個問題,簽名是:
的UpdateModel(對象模型,string [] keys)
我沒有親自升級我的應用程序,所以我不確定實際使用情況。我有興趣瞭解這一點,因爲我使用的是controller.ReadFromRequest。
2
不知道它去了哪裏。你可以儘管推出自己的分機:
公共靜態類MyBindingExtensions {
public static T ReadFromRequest <T> (this Controller controller, string key)
{
// Setup
HttpContextBase context = controller.ControllerContext.HttpContext;
object val = null;
T result = default(T);
// Gaurd
if (context == null)
return result; // no point checking request
// Bind value (check form then query string)
if (context.Request.Form[key] != null)
val = context.Request.Form[key];
if (val == null)
{
if (context.Request.QueryString[key] != null)
val = context.Request.QueryString[key];
}
// Cast value
if (val != null)
result = (t)val;
return result;
}
}
0
你可以重做,像tinyurl.com的東西鏈接?
我也需要這個信息,但可以讓那個超級鏈接工作。