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
中,这是如何工作的?
答案 0 :(得分:2)
我错过了命名空间:
using System.Web.Mvc;
这是因为MapRoute
是一种扩展方法。为了使它出现在对象上,您需要导入扩展方法所在的命名空间。