[英]How to get url desire value in asp.net C#
http://example.com/UI/ProductUI.aspx/GetProductByCategory/1
关于url,我想在单击左导航时在内容页面方法参数中传递url值1。 左侧导航项是我的类别项,我通过循环从我的主页中的类别表中加载了它。 现在,我需要项目值字段,例如categoryId,当单击该项目时通过我的内容页面方法范围。
我的母版页代码如下:
<div class="left">
<%
CategoryManager aCategoryManager=new CategoryManager();
List<Category> categories = aCategoryManager.GetCategories();
foreach (Category category in categories)
{%>
<ul>
<li><a href="/UI/ProductUI.aspx/GetProductByCategory/<%: category.CategoryId %>"><%: category.CategoryName%></a></li>
</ul>
<% }%>
</div>
我的内容页面代码如下:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
int id = Convert.ToInt32(Request.QueryString["CategoryId"]);
GetProductByCategory(id);
}
}
ProductManager aProductManager=new ProductManager();
private void GetProductByCategory(int categoryId)
{
List<Product> products = aProductManager.GetProductByCategory(categoryId);
GridView1.DataSource = products;
GridView1.DataBind();
}
在方法GetProductByCategory
将参数重命名为int id
private void GetProductByCategory(int id)
{
List<Product> products = aProductManager.GetProductByCategory(id);
GridView1.DataSource = products;
GridView1.DataBind();
}
这将允许您保留<li><a href="/UI/ProductUI.aspx/GetProductByCategory/<%: category.CategoryId %>"><%: category.CategoryName%></a></li>
代码照原样,它应该接收传入的CategoryId
作为id
在默认路由中,它正在寻找一个id
参数。
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
否则,@ karl-anderson答案将为您工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.