4

新しいWebAPIMVCプロジェクトを作成しました。APIコントローラーにはパスがhttp://localhost:1234/apiあり、このルートから機能しますが、RegisterRoutesクラスにはデフォルトのルーティングが含まれておらず、次のものが含まれています。

public static void RegisterRoutes(RouteCollection routes)
{
     routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
     routes.MapRoute(
         name: "Default",
         url: "{controller}/{action}/{id}",
         defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );
}

APIのルーティングはどこにありますか?

乾杯

デイブ

4

2 に答える 2

4

Visual Studioプロジェクトテンプレートは、次のようなデフォルトルートを作成します。

routes.MapHttpRoute(
   name: "API Default",
   routeTemplate: "api/{controller}/{id}",
   defaults: new { id = RouteParameter.Optional }
);

これは、ディレクトリに配置されているWebApiConfig.csファイルにあります。App_Start

http://www.asp.net/web-api/overview/web-api-routing-and-actions/routing-in-aspnet-web-api

于 2013-03-16T00:19:17.820 に答える
1

それは別のクラスに住んでいます:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;

namespace HelloWorldApi
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );

            // Uncomment the following line of code to enable query support for actions with an IQueryable or IQueryable<T> return type.
            // To avoid processing unexpected or malicious queries, use the validation settings on QueryableAttribute to validate incoming queries.
            // For more information, visit http://go.microsoft.com/fwlink/?LinkId=279712.
            //config.EnableQuerySupport();

            // To disable tracing in your application, please comment out or remove the following line of code
            // For more information, refer to: http://www.asp.net/web-api
            config.EnableSystemDiagnosticsTracing();
        }
    }
}
于 2013-03-16T00:21:16.157 に答える