简体   繁体   English

如何在ASP.NET C#中获取URL期望值

[英]How to get url desire value in asp.net C#

http://example.com/UI/ProductUI.aspx/GetProductByCategory/1 http://example.com/UI/ProductUI.aspx/GetProductByCategory/1

Regarding the url, I want to pass the url value 1 in my content page method parameter when click in left navigation. 关于url,我想在单击左导航时在内容页面方法参数中传递url值1。 The left navigation item is my Category Item and I load it from my category table in my master page by looping. 左侧导航项是我的类别项,我通过循环从我的主页中的类别表中加载了它。 Now I need the item value field eg categoryId pass my content page method perimeter when click this item. 现在,我需要项目值字段,例如categoryId,当单击该项目时通过我的内容页面方法范围。

My Master page code is below: 我的母版页代码如下:

    <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>

and my content page code is below: 我的内容页面代码如下:

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();
        } 

In the method GetProductByCategory rename the parameter to int id 在方法GetProductByCategory将参数重命名为int id

    private void GetProductByCategory(int id)
    {
        List<Product> products = aProductManager.GetProductByCategory(id);
        GridView1.DataSource = products;
        GridView1.DataBind();
    } 

This would allow you to keep the <li><a href="/UI/ProductUI.aspx/GetProductByCategory/<%: category.CategoryId %>"><%: category.CategoryName%></a></li> code as is and it should pick up the passed in CategoryId as the id 这将允许您保留<li><a href="/UI/ProductUI.aspx/GetProductByCategory/<%: category.CategoryId %>"><%: category.CategoryName%></a></li>代码照原样,它应该接收传入的CategoryId作为id

In the default routes it's looking for an id parameter. 在默认路由中,它正在寻找一个id参数。

    routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );

Otherwise @karl-anderson answer should work for you. 否则,@ karl-anderson答案将为您工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM