当前位置:编程学习 > C#/ASP.NET >>

请问 MVC 生成链接的问题

在MVC中扩展 HtmlHelper的一个方法Pager.
在Pager函数中根据controller,action得到对应的url时始终为空值.
public static MvcHtmlString Pager(this System.Web.Mvc.HtmlHelper html, int pn, int ps, int cnt, int pc, RouteValueDictionary routes)
{
UrlHelper helper = new UrlHelper(html.ViewContext.RequestContext);
string url = helper.Action("Detail", "Article", routes);
// string url = helper.Action("Detail", "Article", new {id="25"}); 也为空值
}


url始终为空值,请问要怎么获取? --------------------编程问答-------------------- 1.你这个方法没有返回值编译能通过吗?
2.你仔细看URLHelper构造函数的解释,使用指定请求上下文创建新的URLHelper的实例,而你这边是用的this HTMLHelper 其实就是当前类。。。 该对象为你自定义的又没有赋值。去实例URLHelper当然为空了。
页面初始化请求的时候  用 @Html.ViewContext.RequestContext.HttpContext.Request.Path 或者
@URL.RequestContext.HttpContext.Request.Path  都能拿到当前请求上下文的路劲。  这里 HTML就是HTMLHelper  URL 就是 URLHelper
3.你是需要扩展URLHelper 还是 HTMHelper 你没有分清楚吧。
4.页面上可以直接使用URLHelper类  如:<a href="@URL.Actionr("Index","Home", new { id = 1})">点击我<a/> 生成一个到homeCotroller下面Index 传递参数id=1的链接
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,