在URL重写mvc4剃刀用发动机 [英] url rewriting in mvc4 with razor engine

查看:131
本文介绍了在URL重写mvc4剃刀用发动机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要重写以下网址 -

I want to rewrite following url -

的http://本地主机:99 /产品/ CategoryLevel类别ID = 65安培;产品名称=维生素

的http://本地主机:99 /产品/维生素

(或)

的http://本地主机:99 /产品/ CategoryLevel /维生素

(或)

的http://本地主机:99 /维生素

(或)如何删除(或)隐藏的URL(被展示给用户)查询字符串?

(or) how to remove (or) hide the querystring from the url (that was shown to the users)?

我尝试使用URL重写模块(IIS)和asp.net路由和搜索的互联网解决方案,但我没有找到合适的解决方案对于​​这一点,请提出任何解决方案。

I tried using url rewrite module(iis) and asp.net routing and search for the solution in the internet,but i didn't find right solution for this,please suggest any solutions.

推荐答案

您必须映射之前,这条航线的 所有其他路径映射(路线的顺序计算):

You must map this route before all the other route mappings (routes are evaluated in order):

routes.MapRoute(
  name: "Product", // any name meaningful for you is right
  url: "Product/{productName}",
   defaults: new { controller = "Product", action = "CategoryLevel" }
);

这个路由将捕获所有看起来像这样的网址:

This route will catch all the URLS that look like this:

http://myserver/Product/X

不管X是。如果你这样做,你的动作应该是这样的:

whatever X is. If you do so, your action should look like this:

public ActionResult CategoryLevel(string productName)

注:该参数名称必须与路由映射段匹配:产品名称

所以,当用户键入:

http://myserver/Product/Vitamins

行动 CategoryLevel 将被执行,并且将获得产品名称参数的值维生素

the action CategoryLevel will be executed, and it will receive the productName parameter with the value "Vitamins"

问题是,如果你有一个动作列表,你希望被调用像这样

The problem is that if you have an action List which you expect to be invoked like this

http://myserver/Product/List

路线将映射,并会调用与 CategoryLevel 行动产品名称 = 列表

要避免这一点,你可以使用这条路线:

To avoid this you can use this route:

routes.MapRoute(
  name: "Product", // any name meaningful for you is right
  url: "ViewProduct/{productName}",
   defaults: new { controller = "Product", action = "CategoryLevel" }
);

这将是不同于其他的,任何将正常工作。具体这个方法的网址会是这样的:

Which will be different from the others, and anything will work fine. The URLs specific for this method will look like this:

http://myserver/ViewProduct/TheProductName

和预期的其他路线会工作。

and the other routes will work as expected.

顺便说一句:你应该有产品的具体行动,例如查看,而不是 CategoryLevel 。因此,路线和行动将是这样的:

By the way: you should have an specific action for the product, for example View, instead of CategoryLevel. So, the route and the action would look like this:

    routes.MapRoute(
        name: "ViewProduct", // any name meaningful for you is right
        url: "ViewProduct/{productName}",
        defaults: new { controller = "Product", action = "View" }
    );

动作,产品控制器内部:

The action, inside the product controller:

public ActionResult View(string productName)

这条路线是既用于映射用户键入URL到相应的动作,以及利用一些MVC佣工,像 Html.ActionLink 或生成网址 Url.Action 。所以,如果你做这样的事情:

The route is used both for mapping a user typed url to the corresponding action, and for generating URLs by using some of the MVC helpers, like Html.ActionLink or Url.Action. So, if you do something like this:

Url.Action('View', 'Product', new {productName = "Vitamins"} )

你会得到预期的短网址:

you'll get the expected, short URL:

http://myserver/ViewProduct/Vitamins

即。路线图这是一个双向的地图,可以URL映射到动作,反之亦然。

I.e. the route map it's a two-way map that can map URLs to actions and viceversa.

这篇关于在URL重写mvc4剃刀用发动机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆