ASP.NET MVC3路由REST服务控制器 [英] ASP.NET MVC3 routing REST services to controller

查看:110
本文介绍了ASP.NET MVC3路由REST服务控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

欲路由REST服务网址以下列方式:

I want to route REST service url in the following way:

/User/Rest/ -> UserRestController.Index()
/User/Rest/Get -> UserRestController.Get()

/User/ -> UserController.Index()
/User/Get -> UserController.Get()

所以基本上我在url制作硬codeD例外休息。

So basically I'm making a hardcoded exception for Rest in the url.

我不是很熟悉,MVC路由。所以,这将是实现这一目标的好方法?

I'm not very familiar with MVC routing. So what would be a good way to achieve this?

推荐答案

无论您注册的路线,常用于global.ascx

Wherever you register your routes, commonly in global.ascx

        routes.MapRoute(
            "post-object",
            "{controller}",
            new {controller = "Home", action = "post"},
            new {httpMethod = new HttpMethodConstraint("POST")}
        );

        routes.MapRoute(
            "get-object",
            "{controller}/{id}",
            new { controller = "Home", action = "get"},
            new { httpMethod = new HttpMethodConstraint("GET")}
            );

        routes.MapRoute(
            "put-object",
            "{controller}/{id}",
            new { controller = "Home", action = "put" },
            new { httpMethod = new HttpMethodConstraint("PUT")}
            );

        routes.MapRoute(
            "delete-object",
            "{controller}/{id}",
            new { controller = "Home", action = "delete" },
            new { httpMethod = new HttpMethodConstraint("DELETE") }
            );


        routes.MapRoute(
            "Default",                          // Route name
            "{controller}",       // URL with parameters
            new { controller = "Home", action = "Index" }  // Parameter defaults
            ,new[] {"ToolWatch.Presentation.API.Controllers"}
        );

在控制器

public ActionResult Get() { }
public ActionResult Post() { }
public ActionResult Put() { }
public ActionResult Delete() { }

这篇关于ASP.NET MVC3路由REST服务控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆