使用剃刀引擎在 mvc4 中重写 url [英] url rewriting in mvc4 with razor engine

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

问题描述

我想重写以下网址 -

I want to rewrite following url -

http://localhost:99/Product/CategoryLevel?CategoryId=65&ProductName=Vitamins

http://localhost:99/Product/Vitamins,

(或)

http://localhost:99/Product/CategoryLevel/Vitamins

(或)

http://localhost:99/Vitamins

(或)如何从 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" }
);

此路由将捕获所有如下所示的 URL:

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)

注意:参数名称必须与路由映射中的段匹配:productName

因此,每当用户输入:

http://myserver/Product/Vitamins

动作 CategoryLevel 将被执行,它会收到值为 "Vitamins"

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

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

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

http://myserver/Product/List

路由将映射它,并使用 productName = "List"

the route will map it and will invoke the CategoryLevel action with the productName = "List"

为避免这种情况,您可以使用以下路线:

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

这将与其他人不同,任何事情都可以正常工作.特定于此方法的 URL 将如下所示:

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.

顺便说一句:您应该对产品有一个特定的操作,例如View,而不是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 帮助程序生成 URL,例如 Html.ActionLinkUrl.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.

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

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