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

user3166404 picture user3166404 · Feb 4, 2014 · Viewed 14.1k times · Source

url.action is :

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

it works fine but i dont want to display the qyery string in url

url is:

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 is: routes.MapRoute( name: "Home", url: "{controller}/{action}", defaults: new { controller = "Home", action = "Index" } );

ActionResult in controller is:`

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

Answer

ramiramilu picture ramiramilu · Feb 4, 2014

Here goes descriptive solution -

First you need to have proper route -

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

Then you can have controller action defined in this way.

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

Finally the link in this way -

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

URL generated - http://localhost:5738/Product/CategoryLevel/1/Rami%20Vemula

And when you click on link, you will get values as shown below -

enter image description here