具有Web Api和Controller路由的Url.Action

时间:2014-07-21 15:27:29

标签: asp.net-mvc asp.net-mvc-4 asp.net-web-api asp.net-mvc-routing

我有2个控制器,其中1个是WebApi:

public class ListController : ApiController
{        
    public object Remove(string ListId, List<string> ItemIds)
    {
        //removed
    }
}

public class ListController : Controller
{        
    public object Remove(string ListId, List<string> ItemIds)
    {
        //removed
    }
}

我的路线在Global.asax中注册如下:

WebApiConfig.Register(GlobalConfiguration.Configuration);
RouteConfig.RegisterRoutes(RouteTable.Routes);

我的WebApi路线定义为:

config.Routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "api/{controller}/{action}/{id}",
    defaults: new { id = RouteParameter.Optional }
);

通过@ Url.Action(“删除”,“列表”)生成网址时,我得到的路径是'/ list / remove'。我期望选择WebApi路由('/ api / list / remove'),因为WebApi路由在其他路由之前注册。

如何让@ Url.Action按预期返回WebApi路线?

2 个答案:

答案 0 :(得分:3)

@NgModule({
    imports: [
        AnfiPortailSharedModule,
        FormsModule,
        NgbModule,
        NgxPaginationModule,
        RouterModule.forRoot(requiRoutes, {useHash: true})
    ],
    declarations: [
        SearchComponent,
        ResultsComponent,
        AuthModalContent,
        ClickOutsideDirective
    ],
    entryComponents: [AuthModalContent],
    exports: [],
    providers: [ConfigService, SearchRequisitionService, {
        provide: NgbDateParserFormatter,
        useFactory: customNgbDateParserFormatterFactory
    }],
    schemas: [CUSTOM_ELEMENTS_SCHEMA]
})
export class AnfiPortailRequisitionModule {
}

答案 1 :(得分:1)

对于任何遇到此问题的人都无法正常工作...如果您的网址是/ List / Remove生成的?httproute = DefaultApi

验证您的api路由是否已首先加载!订单至关重要。

我还在我的网络路由配置中添加了约束,以帮助排除api路由:

### Gtk Gui ###
self.sw = Gtk.Switch()

self.sw.connect("notify::active", 
                 self.on_sw_activated)
### Gtk Gui ###

### Function ###
def on_sw_activated(self, switch, gparam):

    if switch.get_active():
        state = "on"
    else:
        state = "off"

    ### This needs to be "threaded" as to not freeze GUI
    while state == "on":
        print("Function is on")
        time.sleep(2)
    else:
        print("Function is off")

### Function ###