gpt4 book ai didi

c# - RouteCollection.MapRoute 在传递给方法时没有出现

转载 作者:太空宇宙 更新时间:2023-11-03 23:41:37 27 4
gpt4 key购买 nike

路由配置:

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 接口(interface):

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?
}

// ...

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

最佳答案

我缺少命名空间:

using System.Web.Mvc;

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

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

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com