本文介绍了MVC3并重写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写一个MVC3应用程序,将需要使用URL重写HTTP中的形式:// [服务器] / [城市] - [状态] / [一些条件] /

I'm writing an MVC3 application that will need to make use of URL rewriting in the form of http://[server]/[City]-[State]/[some term]/ .

据我了解,MVC3包含使用路由引擎{CONTROLER} / {行动} / {ID}这是在Global.asax文件中定义的:

As I understand it, MVC3 contains a routing engine that uses {controler}/{action}/{id} which is defined in the Global.asax file:

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );

    }

传统(在非MVC应用程序),我会用一些URL重写味道脱codeA URL如的来查询字符串,看起来像这样的参数:

Traditionally (in a non-MVC app), I would use some URL rewriting flavor to decode a url such as http://www.myserver.com/City-State/somesearch/ to querystring parameters that look something like this:http://www.myserver.com/city=City&state=State&query=somesearch

请该请求将来自 http://www.myserver.com/Home

可以这样可以在不指定控制器......像这样来完成:

Can this can be accomplished without having to specify a controller... something like this:

routes.MapRoute(
            "Results",
            "{city}-{state}/{searchTerm}",
            new { controller = "Results", action = "Search" }
        );

...或者是不是真的最好有控制器上市?

... or is it really best to have the controller listed?

你如何在MVC3环境处理呢?

How do you handle this in an MVC3 environment?

感谢。

推荐答案

URL重写在asp.net MVC3: -
你可以写code的URL在Global.asax文件重写: -

URL rewriting in asp.net MVC3:-you can write code for url rewriting in Global.asax file :-

       //Default url
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            "Default",
            "",
            new { controller = "Home", action = "Index", id = "" }
        );

      //others url rewriting you want

        RouteTable.Routes.MapRoute(null, "Search/{City_State}/{ID}", new { controller = "Home", action = "Search" });

这篇关于MVC3并重写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!