如何使用url.action在rav中删除mvc4中url中的查询字符串参数

时间:2014-02-04 12:07:32

标签: asp.net-mvc asp.net-mvc-4 razorengine url.action urlrewriting.net

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`

路由配置为: 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);
        }`

2 个答案:

答案 0 :(得分:2)

这里是描述性的解决方案 -

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

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)

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

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

enter image description here

答案 1 :(得分:0)

试试这个,

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