传递给方法时,RouteCollection.MapRoute不会出现

时间:2015-03-05 19:01:39

标签: c# asp.net-mvc-5 asp.net-mvc-routing

RouteConfig

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        // Bootstrap routes

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

        // Field routes

            FieldRoutes fieldRoutes = new FieldRoutes();

            fieldRoutes.DefineCreate( ref routes );
            fieldRoutes.DefineDetails( ref routes );
            fieldRoutes.DefineEdit( ref routes );
            fieldRoutes.DefineDelete( ref routes );

        // ...

ICRUDRoutes界面

public interface ICRUDRoutes
{
    void DefineCreate( ref RouteCollection routes );
    void DefineDetails( ref RouteCollection routes );
    void DefineList( ref RouteCollection routes );
    void DefineEdit( ref RouteCollection routes );
    void DefineDelete( ref RouteCollection routes );
}

FieldRoute类

public class FieldRoutes : ICRUDRoutes
{
    public void DefineCreate( ref RouteCollection routes )
    {
        routes.MapRoute(
            name: "Soft delete a field",
            url: "field/delete/{fieldID}",
            defaults: new { controller = "Field", action = "Delete" },
            constraints: new { id = RoutingConstraints.ID }
        );
        // routes.MapRoute does not exist as a function, but it exists in the RouteConfig, how does this work?
    }

    // ...

routes.MapRoute在将Routecollection作为ref或其他方式传递后不作为函数存在,但它存在于RouteConfig中,这是如何工作的?

1 个答案:

答案 0 :(得分:2)

我错过了命名空间:

using System.Web.Mvc;

这是因为MapRoute是一种扩展方法。为了使它出现在对象上,您需要导入扩展方法所在的命名空间。