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作为ref或其他形式传递后,Routecollection不作为函数存在,但是它存在于RouteConfig中,这是如何工作的?

最佳答案

我缺少名称空间:

using System.Web.Mvc;


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

关于c# - 传递给方法时,RouteCollection.MapRoute不出现,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28885626/

10-13 01:34