繁体   English   中英

如何使用url.action使用razer删除mvc4中url中的querystring参数

[英]how to remove the querystring parameters in url in mvc4 with razer using url.action

url.action是:

<li><a href="@Url.Action("CategoryLevel", "Product", new { CategoryId = @item._categoryId, ProductName = @Html.Raw(item._categoryName) })">@Html.Raw(item._categoryName)</a></li>

它工作正常,但我不想在URL中显示qyery字符串

网址是:

http://localhost:99/Product/CategoryLevel?CategoryId=16&ProductName=Common%20Conditions

i want to display this as


`http://localhost:99/Product/CategoryLevel/16/Common%20Conditions`  (or)`http://localhost:99/Product/CategoryLevel/Common%20Conditions(or)http://localhost:99/Product/Common%20Conditions`

route config是: routes.MapRoute( name: "Home", url: "{controller}/{action}", defaults: new { controller = "Home", action = "Index" } );

控制器中的ActionResult为:

public ActionResult CategoryLevel()
        {
            string ProductName = Request.QueryString["ProductName"];
            ViewBag.ProductName = ProductName;
            int Category = Convert.ToInt32(Request.QueryString["CategoryId"]);
            ViewBag.ParentCategoryId = Category;
            int ParentCategoryId = 0;
            if (Request.QueryString["ParentCategoryId"] != null)
            {
                ParentCategoryId = Convert.ToInt32(Request.QueryString["ParentCategoryId"]);
            }
            Product productInstance = new Product();
            IList<CategoryInfo> categories = new List<CategoryInfo>();
            categories = productInstance.GetCategories(Category, true);
            if (categories.Count == 0)
            {
                return RedirectToAction("NewProducts", "Product", new { @CategoryId = Category,  ProductName = ProductName });
            }

            return View(categories);

        }`another actionresult is`public ActionResult NewProducts(Product instance)
        {
            string ProductName = Request.QueryString["ProductName"];
            instance.BrandName = ProductName;
            int CategoryId = Convert.ToInt32(Request.QueryString["CategoryId"]);
            int BrandId = Convert.ToInt32(Request.QueryString["BrandId"]);
            string SortBy = Convert.ToString(Request.QueryString["sortBy"]);
            if (SortBy != null)
            {
                Session["Sort"] = SortBy;
            }
            Session["NewProductsBrandId"] = BrandId;
            instance.CategoryId = CategoryId;
            instance.BrandId = BrandId;
            instance.SortBy = SortBy;
            return View(instance);
        }`

这里是描述性解决方案-

首先,您需要有正确的路线-

routes.MapRoute(
    name: "ProductDetails",
    url: "{controller}/{action}/{categoryid}/{productname}",
    defaults: new { controller = "Product", action = "CategoryLevel" }
);

然后,您可以通过这种方式定义控制器动作。

public class ProductController : Controller
{
    public ActionResult CategoryLevel(string CategoryId, string ProductName)
    {
        return View();
    }
}

最后以这种方式链接-

@Html.ActionLink("sample","CategoryLevel", "Product", new { CategoryId = 1, ProductName = "Rami Vemula" }, null)

生成的URL- http://localhost:5738/Product/CategoryLevel/1/Rami%20Vemula

当您点击链接时,您将获得如下所示的值-

在此处输入图片说明

这样尝试

使用Html.ActionLink ,而不是a

 @Html.ActionLink("EditUser", "CategoryLevel", "Product", new { Id = m.ID }, new { @class = "hide" })

要么

<a href='CategoryLevel/@m.ID/@m.Name' >@m.Name</a>

暂无
暂无

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

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