开发者

MVC3 URL Encoding?

ASP.NET MVC3/Razor.

I found that when I create an action link, say, like this:

@Html.ActionLink(product.Title, "Detail", "Products", new { id = product.ProductID }, null)

The MVC3 engine creates my product link. For example:

http://myhost/{ActionUrl}/PRODID

However, if my product ID was to contain any special character, it wouldn't be URL encoded.

http://myhost/{ActionUrl}/PROD/ID

Of course, this breaks my routing. My questi开发者_如何学编程ons are:

  1. Should I expect it to automatically url encode values? Is there any way to set that up?
  2. If not, then what is the most cleaner way to encode and decode those? I really do not want to do that in every controller.

Thanks!


If your id contains special characters I would recommend you passing it as a query string and not as part of the path. If not be prepared for a bumpy road. Checkout the following blog post.


I didn't get this to work in the path, but to make this work as a QueryString parameter as @Darin pointed out here is the code:

@Html.ActionLink(product.Title, "Detail", "Products", new { id = product.ProductID }, "")

created the actionLink as a querystring for me like this: Products/Detail?id=PRODUCTID

my route in Global.asax.cs looked like this:

    routes.MapRoute(
        "ProductDetail",
        "Products/Detail",
        new { controller = "Products", action = "Detail" }
        );

In my ProductController:

public ActionResult Detail(string id)
{
    string identifier = HttpUtility.HtmlDecode(id);
    Store.GetDetails(identifier);
    return RedirectToAction("Index", "Home");
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜