简体   繁体   中英

Url rewrite in MVC

i work at a MVC application and i want to make url's more friendly. i was trying to do that using routes but at some url's it doesn't work.

i want a url like http ://localhost:55696/fr/Pages/Lists?pageType=PropertiesList&list=Market to become http: //localhost:55696/fr/(market_in_french)

I was trying with

routes.MapRoute(
    name: "MarketFr",
    url: UrlStrings.ResourceManager.GetString("Market", new CultureInfo(CultureEnum.fr.ToString())),
    defaults: new {controller = "Pages", action = "Lists"}
);

but the result is http://localhost:55696/fr/market?pageType=PropertiesList&list=Market

how can I solve this. The Lists method is defined like this:

public ActionResult Lists(string pageType, string list = "", string viewType = "")

i made the changes:

routes.MapRoute(
                name: "MarketFr",
                url: UrlStrings.ResourceManager.GetString("Market", new CultureInfo(CultureEnum.fr.ToString())),
                defaults: new { controller = "Pages", action = "Lists", pageType = "PropertiesList", list = "Market", viewType = "" }
                );

now it doesn't work at all, my url is like at the begining: http://localhost:55696/en/Pages/Lists?pageType=PropertiesList&list=Market

if I type in the address bar http://localhost:55696/fr/market it lands me to the right page, but when i click the button linked to the

Url.Action("Lists", "Pages", new { pageType = PageTypesEnum.PropertiesList, list = PropertyListViewMode.Market })

in the address bar the url is http://localhost:55696/en/Pages/Lists?pageType=PropertiesList&list=Market

Not clear how you generate such url, so assuming something like Url.Action . For particular case you have specifying all defaults should produce url you like:

    url: "fr/market",    
    defaults: new {controller = "Pages", action = "Lists", 
                   pageType="PropertiesList", list= "Market"}

You have spaces in that URL. Space characters are not allowed .
Replace each space with %20 in your url:

http ://localhost:55696/fr/Pages/Lists?pageType=PropertiesList&list=Market%20to%20become%20http://localhost:55696/fr/(market_in_french)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM