简体   繁体   English

MVC-默认控制器

[英]MVC - Default Controller

I am creating a site where I would like to have dynamically added pages. 我正在创建一个网站,希望在该网站上动态添加页面。 Because of this, I would like to have a simpler URL. 因此,我想要一个更简单的URL。 What I am aiming for is a URL like the following: 我想要的是一个类似以下的URL:

http://www.mysite.com/my-page-url

Rather than a URL like the following: 而不是像下面这样的URL:

http://www.mysite.com/pageController/my-page-url

MVC URLs tend to work like this: |url|/|controller|/|action|/|params| MVC URL倾向于这样工作: |url|/|controller|/|action|/|params|

What I would like to do is have: |url|/|params| 我想做的是: |url|/|params|

For the above URL, if the values of params does not equal the name of a controller, then I would like to pass those params to PageContoller.ProcessDynamicPage. 对于上面的URL,如果params的值不等于控制器的名称,那么我想将这些params传递给PageContoller.ProcessDynamicPage。

Depending on what your default params are, you could create a route constraint 根据您的默认参数是什么,您可以创建路径约束

    routes.MapRoute(
        "Default",
        "{Param1}",
        new { controller = "ProcessDynamicPage", action = "YourAction" },
        new {Param1= @"\d+" }
    );

This would work if your parameter is an integer. 如果您的参数是整数,这将起作用。

If your default params are strings or something and you can't create a regex, or something that can't implement IRouteConstraint your best bet it to create a matching action for each of your controllers that way when it falls through the default you've already attempted to do the matching. 如果您的默认参数是字符串或其他内容,并且您无法创建正则表达式,或者某些无法实现IRouteConstraint的内容,则最好选择为每个控制器创建匹配操作的方式,当其达到默认值时已经尝试进行匹配。

routes.MapRoute(
    "YourController",
    "YourController\{Param1}",
    new { controller = "YourController", action = "YourAction" }
   }
);

routes.MapRoute(
    "YourController2",
    "YourController2\{Param1}",
    new { controller = "YourController2", action = "YourAction" }
     }
);

routes.MapRoute(
    "Default",
    "{Param1}",
    new { controller = "ProcessDynamicPage", action = "YourAction" }          
);

If you have a lot of controllers, you should probably really look into how to create the constraint for what you're expecting. 如果您有很多控制器,则可能应该真正研究如何为期望的内容创建约束。

Using MVC's default routing scheme, http://www.mysite.com/my-page-url will go to the Index action in my-page-url controller. 使用MVC的默认路由方案, http://www.mysite.com/my-page-url将转到my-page-url控制器中的Index操作。 If you want to set the default controller and action, you can do so in your Global.asax.cs file it to something like: 如果要设置默认控制器和操作,则可以在Global.asax.cs文件中将其设置为类似以下内容:

routes.MapRoute(
    "Default",                                              // Route name
    "{action}/{pageName}",                           // URL with parameters
    new { controller = "PageContoller", action = "ProcessDynamicPage", pageName = "" }  // Parameter defaults
);

Update 更新资料

If you need to support additional routes you can create them using the kind of approach that Mark Oreta has suggested. 如果您需要支持其他路线,则可以使用Mark Oreta建议的方法创建它们。 Have a look at Scott Gu's blog and this tutorial . 看看Scott Gu的博客本教程

In IIS 7 you need to look at Handler Mappings. 在IIS 7中,您需要查看处理程序映射。 You can specify a particular extension, eg *.action, to be handled by your default controller. 您可以指定特定的扩展名,例如* .action,由默认控制器处理。 I'm a bit rusty now, but I think it was basically the same idea in IIS 6. In my web.config file for my .Net 2.0 application I have the following section inside the system.webServer tag: 我现在有点生锈,但是我认为它在IIS 6中基本上是相同的想法。在我的.Net 2.0应用程序的web.config文件中,system.webServer标记中包含以下部分:

    <system.webServer>
        <handlers>
            <add name="defaultAction" path="*.action"   
                verb="*" 
                type="SumikinIntercom.Web.Controller.Controller,SumikinIntercom.Web.Controller" 
                resourceType="Unspecified" requireAccess="None" allowPathInfo="true" />
        </handlers>

So any URL with the extension .action is handled by my default Controller class. 因此,任何带有扩展名.action的URL都由我的默认Controller类处理。 Notice the awkward syntax. 请注意尴尬的语法。 I had to specify the fully qualified class name (including the assembly name) followed by a comma then the fully qualified assembly name. 我必须指定完全限定的类名称(包括程序集名称),后跟一个逗号,然后再指定完全限定的程序集名称。

There may be irritating changes of syntax for more up-to-date versions of .Net so watch out for those. 对于.Net的最新版本,语法可能会有令人不愉快的更改,因此请当心。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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