url rewriting in mvc4 with razor engine

心已入冬 提交于 2019-12-01 04:20:29

问题


I want to rewrite following url -

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

with

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

(or)

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

(or)

http://localhost:99/Vitamins

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

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.


回答1:


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

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

public ActionResult CategoryLevel(string productName)

NOTE: The parameter name must match the segment in the route mapping: productName

So, whenever the user types:

http://myserver/Product/Vitamins

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

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

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.

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)

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

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




回答2:


you can use url rewriting middleware ,i used this one to solve mine problem https://docs.microsoft.com/en-us/aspnet/core/fundamentals/url-rewriting?tabs=aspnetcore2x




回答3:


You need to edit your AppData > RouteConfig.cs

add the next lines of code there (below Default)

        routes.MapRoute(
            name: "[Choose a name]",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Product", action = "CategoryLevel", id = UrlParameter.Optional }
        );

And your Controller action should be the following

public ActionResult CategoryLevel(string ID)
{
}


来源:https://stackoverflow.com/questions/21450944/url-rewriting-in-mvc4-with-razor-engine

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!