Jiro Laboratory

C#、ASP.NET、JavaScript、Androidとか

ASP.NET MVCプロジェクト と Web APIプロジェクトの相違点

MVCプロジェクトに後からAPI機能を追加するための参考資料です。

簡単にまとめ

MVCプロジェクト と Web APIプロジェクトには以下の相違点がありました。

  • (Web APIのみ)App_Start / WebApiConfig.cs
  • (Web APIのみ)Areas / Help Page
  • (Web APIのみ)Controllers / ValuesController.cs
  • Global.asax.cs
  • packages.config
  • Web.config

検証環境

バージョン

Microsoft Visual Studio Express 2013 for Web Version 12.0.30723.00 Update 3
Microsoft .NET Framework Version 4.5.51641
ASP.NET および Web ツール 12.3.50717.0

プロジェクトの作成

どちらもWebApplication1という名前で、認証なしで作成。

MVC Web API

相違点

MVCプロジェクトにはjQuery.Validationが含まれていますが、Web APIとは直接関係しないので今回は特に記載しません。

App_Start / WebApiConfig.cs

MVC

-- MVCプロジェクトに、このファイルはありません ---
Web API
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)
        {
            // Web API の設定およびサービス

            // Web API ルート
            config.MapHttpAttributeRoutes();

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

Areasフォルダ(APIプロジェクトのみ)

ここには Help Page というWeb APIのドキュメントが自動生成されています。

Controllers / ValuesController.cs

初期生成される Web API コントローラのサンプルソース

MVC

-- MVCプロジェクトに、このファイルはありません ---
Web API
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace WebApplication1.Controllers
{
    public class ValuesController : ApiController
    {
        // GET api/values
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }

        // GET api/values/5
        public string Get(int id)
        {
            return "value";
        }

        // POST api/values
        public void Post([FromBody]string value)
        {
        }

        // PUT api/values/5
        public void Put(int id, [FromBody]string value)
        {
        }

        // DELETE api/values/5
        public void Delete(int id)
        {
        }
    }
}

Views / Homeフォルダ

ナビゲーションバーの構成が違うので、Web API には存在しないファイルがあります。

ファイル名 Mvc Web API
About.cshtml なし
Contact.cshtml なし
Index.cshtml 差分あり

Views / Shared / _Layout.csthml

前述の通り、ナビゲーションバーに違いがあります。

MVC



Web API



MVC

<div class="navbar-collapse collapse">
    <ul class="nav navbar-nav">
        <li>@Html.ActionLink("ホーム", "Index", "Home")</li>
        <li>@Html.ActionLink("詳細", "About", "Home")</li>
        <li>@Html.ActionLink("連絡先", "Contact", "Home")</li>
    </ul>
</div>
Web API

<div class="navbar-collapse collapse">
    <ul class="nav navbar-nav">
        <li>@Html.ActionLink("ホーム", "Index", "Home", new { area = "" }, null)</li>
        <li>@Html.ActionLink("API", "Index", "Help", new { area = "" }, null)</li>
    </ul>
</div>

Global.asax.cs

Web APIにGlobalConfiguration.Configure(WebApiConfig.Register);
の呼び出しが追加されています。

MVC

namespace WebApplication1
{
    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
        }
    }
}
Web API

namespace WebApplication1
{
    public class WebApiApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            GlobalConfiguration.Configure(WebApiConfig.Register);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
        }
    }
}

packages.config

Web APIプロジェクトにはMicrosoft.AspNet.WebApi関連のパッケージが追加されています。

MVC

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="Antlr" version="3.4.1.9004" targetFramework="net451" />
  <package id="bootstrap" version="3.0.0" targetFramework="net451" />
  <package id="jQuery" version="1.10.2" targetFramework="net451" />
  <package id="jQuery.Validation" version="1.11.1" targetFramework="net451" />
  <package id="Microsoft.AspNet.Mvc" version="5.2.0" targetFramework="net451" />
  <package id="Microsoft.AspNet.Mvc.ja" version="5.2.0" targetFramework="net451" />
  <package id="Microsoft.AspNet.Razor" version="3.2.0" targetFramework="net451" />
  <package id="Microsoft.AspNet.Razor.ja" version="3.2.0" targetFramework="net451" />
  <package id="Microsoft.AspNet.Web.Optimization" version="1.1.3" targetFramework="net451" />
  <package id="Microsoft.AspNet.Web.Optimization.ja" version="1.1.3" targetFramework="net451" />








  <package id="Microsoft.AspNet.WebPages" version="3.2.0" targetFramework="net451" />
  <package id="Microsoft.AspNet.WebPages.ja" version="3.2.0" targetFramework="net451" />
  <package id="Microsoft.jQuery.Unobtrusive.Validation" version="3.2.0" targetFramework="net451" />
  <package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net451" />
  <package id="Modernizr" version="2.6.2" targetFramework="net451" />
  <package id="Newtonsoft.Json" version="6.0.3" targetFramework="net451" />
  <package id="Respond" version="1.2.0" targetFramework="net451" />
  <package id="WebGrease" version="1.5.2" targetFramework="net451" />
</packages>
Web API

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="Antlr" version="3.4.1.9004" targetFramework="net451" />
  <package id="bootstrap" version="3.0.0" targetFramework="net451" />
  <package id="jQuery" version="1.10.2" targetFramework="net451" />

  <package id="Microsoft.AspNet.Mvc" version="5.2.0" targetFramework="net451" />
  <package id="Microsoft.AspNet.Mvc.ja" version="5.2.0" targetFramework="net451" />
  <package id="Microsoft.AspNet.Razor" version="3.2.0" targetFramework="net451" />
  <package id="Microsoft.AspNet.Razor.ja" version="3.2.0" targetFramework="net451" />
  <package id="Microsoft.AspNet.Web.Optimization" version="1.1.3" targetFramework="net451" />
  <package id="Microsoft.AspNet.Web.Optimization.ja" version="1.1.3" targetFramework="net451" />
  <package id="Microsoft.AspNet.WebApi" version="5.2.0" targetFramework="net451" />
  <package id="Microsoft.AspNet.WebApi.Client" version="5.2.0" targetFramework="net451" />
  <package id="Microsoft.AspNet.WebApi.Client.ja" version="5.2.0" targetFramework="net451" />
  <package id="Microsoft.AspNet.WebApi.Core" version="5.2.0" targetFramework="net451" />
  <package id="Microsoft.AspNet.WebApi.Core.ja" version="5.2.0" targetFramework="net451" />
  <package id="Microsoft.AspNet.WebApi.HelpPage" version="5.2.0" targetFramework="net451" />
  <package id="Microsoft.AspNet.WebApi.WebHost" version="5.2.0" targetFramework="net451" />
  <package id="Microsoft.AspNet.WebApi.WebHost.ja" version="5.2.0" targetFramework="net451" />
  <package id="Microsoft.AspNet.WebPages" version="3.2.0" targetFramework="net451" />
  <package id="Microsoft.AspNet.WebPages.ja" version="3.2.0" targetFramework="net451" />

  <package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net451" />
  <package id="Modernizr" version="2.6.2" targetFramework="net451" />
  <package id="Newtonsoft.Json" version="6.0.3" targetFramework="net451" />
  <package id="Respond" version="1.2.0" targetFramework="net451" />
  <package id="WebGrease" version="1.5.2" targetFramework="net451" />
</packages>

Web.config

Web APIに以下の記述が追加されていました。

  <system.webServer>
    <handlers>
      <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
      <remove name="OPTIONSVerbHandler" />
      <remove name="TRACEVerbHandler" />
      <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
    </handlers>
  </system.webServer>