如何使用url.action的Razer删除查询字符串参数的URL在mvc4 [英] how to remove the querystring parameters in url in mvc4 with razer using url.action
问题描述
url.action是:
<立GT;< A HREF =@ Url.Action(CategoryLevel,产品,新{=类别ID @ item._categoryId,产品名称= @ Html.Raw (item._categoryName)})> @ Html.Raw(item._categoryName)LT; / A>< /李>
它工作正常,但我不想显示的URL字符串qyery
网址是:
<$c$c>http://localhost:99/Product/CategoryLevel?CategoryId=16&ProductName=Common%20Conditions$c$c>
我要显示这是
`HTTP://本地主机:99 /产品/ CategoryLevel / 16 /通用%20Conditions` (or)`http://localhost:99/Product/CategoryLevel/Common%20Conditions(or)http://localhost:99/Product/Common%20Conditions`
路由配置为:
routes.MapRoute(
名称:家,
网址:{控制器} / {行动},
默认:新{控制器=家,行动=索引}
);
在的ActionResult控制器是:`
公众的ActionResult CategoryLevel()
{
字符串产品名称=的Request.QueryString [产品名称];
ViewBag.ProductName =产品名称;
INT类别= Convert.ToInt32(的Request.QueryString [的CategoryId]);
ViewBag.ParentCategoryId =类别;
INT ParentCategoryId = 0;
如果(的Request.QueryString [ParentCategoryId]!= NULL)
{
ParentCategoryId = Convert.ToInt32(的Request.QueryString [ParentCategoryId]);
}
产品productInstance =新产品();
IList的&LT; CategoryInfo&GT;类别=新的List&LT; CategoryInfo&GT;();
类别= productInstance.GetCategories(分类,真实);
如果(categories.Count == 0)
{
返回RedirectToAction(新品推荐,产品,新的{@CategoryID =分类,产品名称产品名称=});
} 返回查看(类别); }`其他的ActionResult is`public的ActionResult新品展示(产品实例)
{
字符串产品名称=的Request.QueryString [产品名称];
instance.BrandName =产品名称;
INT类别ID = Convert.ToInt32(的Request.QueryString [的CategoryId]);
INT BrandId = Convert.ToInt32(的Request.QueryString [BrandId]);
字符串SortBy = Convert.ToString(的Request.QueryString [sortBy]);
如果(SortBy!= NULL)
{
会话[排序] = SortBy;
}
会话[NewProductsBrandId] = BrandId;
instance.CategoryId =类别ID;
instance.BrandId = BrandId;
instance.SortBy = SortBy;
返回视图(实例);
}`
下面去描述的解决方案 -
首先你需要有合适的路由 -
routes.MapRoute(
名称:产品详细
网址:{控制器} / {行动} / {类别id} / {}产品名称,
默认:新{控制器=产品,行动=CategoryLevel}
);
然后你就可以拥有这样定义的控制器动作。
公共类ProductController的:控制器
{
公众的ActionResult CategoryLevel(字符串categoryId,字符串产品名称)
{
返回查看();
}
}
最后,在这种方式的链路 -
@ Html.ActionLink(样本,CategoryLevel,产品,新的{类别ID = 1,产品名称=拉米Vemula},NULL)
URL生成 - 的http://本地主机:5738 /产品/ CategoryLevel / 1 /拉米%20Vemula
当你点击链接,你将如下图所示获取值 -
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);
}`
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 -
这篇关于如何使用url.action的Razer删除查询字符串参数的URL在mvc4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!