ASP.NET MVC etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster
ASP.NET MVC etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster

8 Temmuz 2016 Cuma

MVC project Add Web Api

Update the MVC project

Use Nuget to get the newest Web API.
Project - Right click - Manage Nuget Packages - Search for Web API (Microsoft ASP.NET Web API ...) and install it to your MVC project.
Then you still need to get Web API routing to work. From

Add WebApiConfig.cs to the App_Start/ folder
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;

namespace WebApplication1
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // TODO: Add any additional configuration code.

            // Web API routes
            config.MapHttpAttributeRoutes();

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

        // WebAPI when dealing with JSON & JavaScript!
        // Setup json serialization to serialize classes to camel (std. Json format)
        var formatter = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
        formatter.SerializerSettings.ContractResolver =
            new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver();

        }
    }
}
If you have an MVC Project it will have Global.asax.cs, add the new routes. Order of the Global.asax.cs routes is critical. Note there are outdated examples which use WebApiConfig.Register
Add this line to Global.asax.cs: GlobalConfiguration.Configure(WebApiConfig.Register);
        // Default stuff
        AreaRegistration.RegisterAllAreas();

        // Manually installed WebAPI 2.2 after making an MVC project.
        GlobalConfiguration.Configure(WebApiConfig.Register); // NEW way
        //WebApiConfig.Register(GlobalConfiguration.Configuration); // DEPRECATED

        // Default stuff
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);

WebAPI Help


To verify WebAPI is working:

To the controllers folder -> Add new item -> Web API Controller Class.
public class TestController : ApiController
{
    //public TestController() { }

    // GET api/<controller>
    public IEnumerable<string> Get()
    {
        return new string[] { "value1", "value2" };
    }

    // GET api/<controller>/5
    public string Get(int id)
    {
        return "value";
    }
    //...
}
Now you can test in IE/FF/Chrome as usual, or in the JavaScript consoles for non-get testing.
(With just the controller in the URL it will call the GET() action in the new Web API Controller, it's automatically mapped to methods/actions depending on the REST e.g. PUT/POST/GET/DELETE. You don't need to call them by action like in MVC) The URL directly:
http://localhost:PORT/api/CONTROLLERNAME/
Alternatively use jQuery to query the controller. Run the project, Open the console (F12 in IE) and try run an Ajax query. (Check your PORT & CONTROLLERNAME)
$.get( "http://localhost:PORT/api/CONTROLLERNAME/", function( data ) {
    //$( ".result" ).html( data );
    alert( "Get data received:" + data);
});

24 Şubat 2015 Salı

ASP.NET MVC Life Cycle


1) HTTP Request: Sizin her ASP.NET MVC uygulamasını görüntülemek istemeniz bir request(istek) tir.Bu istediğinizi HTTP üzerinden IIS tarafından alınır. Her yaptığınız istek Server tarafından bir yanıtlason bulması gerekir.

2) Routing: ASP.NET MVC uygulamasını her istek yaptığınızda, yaptığınız yanıt UrlRoutingModule HTTP Module tarafından durdurulur. UrlRoutingModule bir isteği durdurduğu zaman, gelen istek RouteTable’dan hangi Controller tarafından üstleneceğine karar verilir.

3) Controller: RouteTable’dan gelen route bilgisine göre Controller hangi Action’ı çalıştıracaksa o
View çalıştırılır. View, Controller tarafından render edilmez. Controller tarafından geriye ViewResult
döndürülür.

4) ViewResult: ViewResult, View’i render etmek için aktif View Engine’i çağırır.

5) ViewEngine : Bir CSHTML dosyayı oluşturduğunuzda içerisindeki script ve markuplar, Razor View Engin tarafından bazı ASP.NET API’lerini sayfalarınızı HTML’e çevirmek için kullanır.

6) View: View Engine tarafından HTML’e çevirilen kodlar kullanıcıya sunulur.


7) Response: HTTP üzerinden View kullanıcıya gösterilir