Simple WEB API REST Project

后端 未结 4 1064
天命终不由人
天命终不由人 2021-02-02 12:06

What is the best way to create a simple REST API in Visual Studio 2013 using ASP.NET MVC.

I just want something with controllers and route config along with any other ba

相关标签:
4条回答
  • 2021-02-02 12:26

    I suppose the best way for your project is to use WebApi even with simple HTML page, but... jQuery and even something like Knockout.JS or Angular, this is MUST HAVE for any modern project.

    0 讨论(0)
  • 2021-02-02 12:27

    You can remove the MVC component if you wish, and just use Web API 2 stuff. Add your own custom config bit:

            //attribute routing - lets me put any route any method in any darn file.
            //this sets you free.  I could never see doing it any other way.
            config.MapHttpAttributeRoutes();
    

    So that you can create any route you wish on the fly, in any file you create as a Controller.

    public class MyWeirdController : ApiController {
        //via REST, get object //method-route defined here as I want it
        [HttpGet] [Route("~/my/custom/route/someobject/{objectid}")]    
        public CustomObject GonnaGetCustomObject(int objectid) {
    
           .... use whatever 
          var obj = new CustomObject();
          obj.SetSomething = true;
    
         return obj
        }
    }
    

    You don't need MVC. I use WebAPI2 with Linq2SQL (Entities is more of a PITA when I want simple) - but that part isn't needed either. L2SQL entities are just faster/easier to stand up and get running with their modeling classes - but any class or whatever you want, send it.

    I usually use Knockout if I want to MVVM it on the javascript side. With KO.Mapping -- I can, with one line of code - receive that CustomOBject and have a JS object to work with and instantly bind to elements on the page. The toughest part of all of this is learning Knockout. Custom attribute writing with WebAPI2 is crazy easy and fun.

    In short, WebAPI2 with custom attribute routing is all you need to stand up a REST client in any way you want. Break out your "controllers" any way you wish with any methods/returns/types as you want. You can make it as simple as you want, or start tossing in things like Knockout and L2SQL (or EF) to type-bind things even closer.

    0 讨论(0)
  • 2021-02-02 12:34

    It's all in the template you select.

    1. In the New Project dialog, select ".NET Framework 4.5" if not already selected
    2. Navigate to Installed > Templates > Visual C# > Web, then "ASP.NET Web Application" and select OK.
    3. On the dialog to select the project type, select "Empty", but then be sure to check "Web API" under "Add folders and core references.

    Now you'll have the bare minimums created.

    See: http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api

    0 讨论(0)
  • 2021-02-02 12:37

    Depending on what parts of ASP.NET you want, you could check out Nancy at https://github.com/thecodejunkie/Nancy. It can be hosted within ASP.NET and is pretty light-weight and provides a simple routing with options to plug in different view technologies (Razor, Spark, SimpleView and more).

    0 讨论(0)
提交回复
热议问题