我怎么才能写
Response.Write("<a href='/someurl'>Some Text</a>");
而不是
Response.Write(Html.ActionLink(...));
我之后的原因是因为我正在写一个Pagination ViewUserControl。在控件中我希望它显示页码,下一页和上一页等。
当我使用以下代码时
Response.Write(Html.ActionLink(page.ToString(), HttpContext.Current.Request.RawUrl, new { page = page }));
链接写为http://localhost:61293/customers/customers/System.Web.Mvc.UrlParameter/page2
,显然不正确。
HttpContext.Current.Request.RawUrl == "/customers/"
。我原本期望得到的Url是/customers/page2
而不是实际写出来的。
我的路线设置如下:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}/page{page}", // URL with parameters
new
{
controller = "Home",
action = "Index",
id = UrlParameter.Optional,
page = UrlParameter.Optional
} // Parameter defaults
);
任何人都能解释一下吗?
答案 0 :(得分:1)
根据您的更新,您不应将HttpContext.Current.Request.RawUrl
用作ActionLink
的操作。
http://msdn.microsoft.com/en-us/library/dd505040(v=VS.90).aspx
public static string ActionLink(
this HtmlHelper htmlHelper,
string linkText,
string actionName,
object routeValues
)
如您所见,HttpContext.Current.Request.RawUrl
将无法与您的任何操作相匹配。试着写:
Html.ActionLink(page.ToString(),
"TheNameOfTheActionMethod",
new { page = page });
答案 1 :(得分:0)
我认为您的路由设置不正确 - 可能是您的某个路由中错误位置的参数。我这样说的原因是因为看起来UrlParameter类本身的ToString方法被调用,导致输出类的名称。
分页路由(来自评论) - 这假设“list”操作是“index”,并且您不希望索引操作出现在路径中。请注意,它从当前请求继承控制器,因为按照惯例操作,您不需要提供它。完全未经测试。
routes.MapRoute(
"Pager", // Route name
"{controller}/page{page}", // URL with parameters
new
{
controller = "Home",
action = "Index",
page = UrlParameter.Optional
} // Parameter defaults
);
用作:
<%= Html.RouteLink( page, "Pager", new { page = page }, null ) %>
答案 2 :(得分:0)
Url.Action("ActionMethodName", new { page = i });
答案 3 :(得分:0)
克里斯,
通过阅读上面的“澄清”和评论,我认为你只是使事情过于复杂。
行之间可识别的最明显的事情是,您需要一些可重复使用的小部件,向您显示可分页的客户列表。
为什么不简单地说:
CustomerController
,并Index()
作为显示客户列表的操作"customers"
和"customers/page{page}"
映射到CustomerController.Index()
Html.RenderAction("Index", "Customer", new {page = page})
。按照这种方式,所有繁重的工作(包括URL解析)都将由基础设施为您完成,您将获得“可重复使用的小部件”或“控制”。
至于提到的HttpContext.Current.Request.RawUrl
内容 - 您必须决定:您要么使用ASP.NET 路由,要么自己构建所有路由。因为这些技术在某种程度上相互排斥。
P.S。无论如何,关于你想要达到的目标的更多细节将帮助我们帮助你。