Advertisement
gsscoder

ASP.NET MVC 3 RESTful

May 6th, 2012
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.70 KB | None | 0 0
  1. /*HomeController.cs*/
  2. using System.Web.Mvc;
  3. using RiaLibrary.Web;
  4.  
  5. namespace RESTSample.Controllers
  6. {
  7.     public class HomeController : Controller
  8.     {
  9.         [HttpGet, Url("")]
  10.         public JsonResult Index()
  11.         {
  12.             return Json(new { Message = "Hello ASP.NET MVC 3 RESTful WS!" }, JsonRequestBehavior.AllowGet);
  13.         }
  14.  
  15.         [HttpGet, Url("{who?}")]
  16.         public JsonResult SayHello(string who)
  17.         {
  18.             return Json(new { Message = "Hello, " + who + "!" }, JsonRequestBehavior.AllowGet);
  19.         }
  20.     }
  21. }
  22.  
  23. /*Global.asax*/
  24. using System.Web.Mvc;
  25. using System.Web.Routing;
  26. using RiaLibrary.Web;
  27.  
  28. namespace RESTSample
  29. {
  30.     // Note: For instructions on enabling IIS6 or IIS7 classic mode,
  31.     // visit http://go.microsoft.com/?LinkId=9394801
  32.  
  33.     public class MvcApplication : System.Web.HttpApplication
  34.     {
  35.         public static void RegisterGlobalFilters(GlobalFilterCollection filters)
  36.         {
  37.             filters.Add(new HandleErrorAttribute());
  38.         }
  39.  
  40.         public static void RegisterRoutes(RouteCollection routes)
  41.         {
  42.             routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
  43.  
  44.             routes.MapRoutes();
  45.  
  46.             routes.MapRoute(
  47.                 "Default", // Route name
  48.                 "{controller}/{action}/{id}", // URL with parameters
  49.                 new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
  50.             );
  51.  
  52.         }
  53.  
  54.         protected void Application_Start()
  55.         {
  56.             AreaRegistration.RegisterAllAreas();
  57.  
  58.             RegisterGlobalFilters(GlobalFilters.Filters);
  59.             RegisterRoutes(RouteTable.Routes);
  60.         }
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement