错误判定定义及System.Web.Routing.RouteValueDictionary 没有扩展方法
我正在 4GuysFromRolla 网站上阅读 Scott Mitchell 编写的有关在 ASP.NET MVC 2 中对数据网格进行排序和分页的教程。我收到错误 CS1061:“System.Web.Routing.RouteValueDictionary”不包含“AddQueryStringParameters”的定义,并且找不到接受“System.Web.Routing.RouteValueDictionary”类型的第一个参数的扩展方法“AddQueryStringParameters” (您是否缺少 using 指令或程序集引用?)。我不确定是否需要添加 dll 引用或其他内容。请有人建议如何解决这个问题,提前致谢。我也下载了demo,没有问题。错误位于 PagerLink.ascx 文件中..routeData.AddQueryStringParameters(); // 错误指向此处
RouteValueDictionaryExtensions.cs 看起来像这样(这是帮助程序文件)...
using System.Web.Routing;
namespace Web
{
public static class RouteValueDictionaryExtensions
{
public static RouteValueDictionary
AddQueryStringParameters(this RouteValueDictionary dict)
{
var querystring = HttpContext.Current.Request.QueryString;
foreach (var key in querystring.AllKeys)
if (!dict.ContainsKey(key))
dict.Add(key, querystring.GetValues(key)[0]);
return dict;
}
public static RouteValueDictionary ExceptFor(this RouteValueDictionary
dict, params string[] keysToRemove)
{
foreach (var key in keysToRemove)
if (dict.ContainsKey(key))
dict.Remove(key);
return dict;
}
}
}
Global.asax.cs 看起来像这样...
enter code here
namespace GridDemosMVC
{
// Note: For instructions on enabling IIS6 or IIS7 classic mode,
// visit http://go.microsoft.com/?LinkId=9394801
public class MvcApplication : System.Web.HttpApplication
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id =
UrlParameter.Optional } // Parameter defaults
);
}
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterRoutes(RouteTable.Routes);
}
}
}
我还使用 Dynamic.cs 文件,该文件可以在 microsoft 上下载。
I am going through tutorial at 4GuysFromRolla website regarding Sorting and Paging a Grid of Data in ASP.NET MVC 2 by Scott Mitchell. I am receiving an error CS1061: 'System.Web.Routing.RouteValueDictionary' does not contain a definition for 'AddQueryStringParameters' and no extension method 'AddQueryStringParameters' accepting a first argument of type 'System.Web.Routing.RouteValueDictionary' could be found (are you missing a using directive or an assembly reference?). I am not sure if I need to add a dll reference or something else. Please could someone advise how to solve this thanks in advance. Also I downloaded the demo and there is no problem. error is in PagerLink.ascx file..routeData.AddQueryStringParameters(); // error pointing here
RouteValueDictionaryExtensions.cs looks like this (this is the helper file)...
using System.Web.Routing;
namespace Web
{
public static class RouteValueDictionaryExtensions
{
public static RouteValueDictionary
AddQueryStringParameters(this RouteValueDictionary dict)
{
var querystring = HttpContext.Current.Request.QueryString;
foreach (var key in querystring.AllKeys)
if (!dict.ContainsKey(key))
dict.Add(key, querystring.GetValues(key)[0]);
return dict;
}
public static RouteValueDictionary ExceptFor(this RouteValueDictionary
dict, params string[] keysToRemove)
{
foreach (var key in keysToRemove)
if (dict.ContainsKey(key))
dict.Remove(key);
return dict;
}
}
}
Global.asax.cs looks like this...
enter code here
namespace GridDemosMVC
{
// Note: For instructions on enabling IIS6 or IIS7 classic mode,
// visit http://go.microsoft.com/?LinkId=9394801
public class MvcApplication : System.Web.HttpApplication
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id =
UrlParameter.Optional } // Parameter defaults
);
}
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterRoutes(RouteTable.Routes);
}
}
}
I am also using Dynamic.cs file which is available at microsoft to download.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要使用扩展方法为命名空间添加
using
语句和<%@ Import
指令。或者,您可以将扩展方法移至项目的命名空间中。
You need to add a
using
statement and<%@ Import
directive for the namespace with the extension method.Alternatively, you can move the extension method into your project's namespace.
添加到PagerLink.ascx文件中
<%@ Import Namespace="你的项目命名空间" %>
add in PagerLink.ascx file
<%@ Import Namespace="your project name space" %>
在 2 个用户控件 (
PagerLink.ascx
和SmartLink.ascx
) 中引用命名空间 web,如下所示。<%@ Import Namespace="Web"%>
如果您更改了现有命名空间,请使用项目的适当命名空间。
Refer the namespace web in 2 user controls (
PagerLink.ascx
&SmartLink.ascx
) as shown below.<%@ Import Namespace="Web"%>
If you have changed the existing namespace, use the appropriate namespace of your project.