0

に設定するには、どのように私はこのように、WEBAPIにURLが:URLは、MVC

public class RouteConfig 
{ 

    public static void RegisterRoutes(RouteCollection routes) 
    { 
     routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 

     routes.MapRoute(
      name: "DefaultApi", 
      url: "DefaultApi/{action}/{id}", 
      defaults: new { controller = "Guestbook", action = "Index", id = UrlParameter.Optional, PageID = 1067 } 
     ); 

    } 
} 

public class WebApiConfig 
{ 
    public static void Register(HttpConfiguration config) 
    { 

     // Web API configuration and services 

     // Web API routes 
     config.MapHttpAttributeRoutes(); 

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

    }  
} 

とグローバル:

http://Dynamicweb8724.nl/webapi/NavToDW/?process=" 

とMVCプロジェクトで

私はこのファイルを持っています.asaxファイル:

public class Global : System.Web.HttpApplication 
{ 
    public void Application_Start(object sender, EventArgs e) 
    { 
     AreaRegistration.RegisterAllAreas(); 
     GlobalConfiguration.Configure(WebApiConfig.Register); 
     RouteConfig.RegisterRoutes(RouteTable.Routes); 

     ViewEngines.Engines.Clear(); 
     ViewEngines.Engines.Add(new Dynamicweb.AspNet.Views.RazorViewEngine()); 
     ViewEngines.Engines.Add(new Dynamicweb.AspNet.Views.WebFormViewEngine()); 
     // Fires when the application is started 
     Dynamicweb.Frontend.GlobalAsaxHandler.Application_Start(sender, e); 

     GlobalConfiguration.Configuration.EnsureInitialized(); 
    } 

    public void Session_Start(object sender, EventArgs e) 
    { 
     // Fires when the session is started 
     Dynamicweb.Frontend.GlobalAsaxHandler.Session_Start(sender, e); 
    } 

    public void Application_BeginRequest(object sender, EventArgs e) 
    { 
     // Fires at the beginning of each request 
     //GlobalAsax.Application_BeginRequest(sender, e); 
    } 

    public void Application_AuthenticateRequest(object sender, EventArgs e) 
    { 
     // Fires upon attempting to authenticate the use 
     Dynamicweb.Frontend.GlobalAsaxHandler.Application_AuthenticateRequest(sender, e); 
    } 

    public void Application_Error(object sender, EventArgs e) 
    { 
     // Fires when an error occurs 
     Dynamicweb.Frontend.GlobalAsaxHandler.Application_Error(sender, e); 
    } 

    public void Session_End(object sender, EventArgs e) 
    { 
     // Fires when the session ends 
     Dynamicweb.Frontend.GlobalAsaxHandler.Session_End(sender, e); 
    } 

    public void Application_End(object sender, EventArgs e) 
    { 
     // Fires when the application ends 
     Dynamicweb.Frontend.GlobalAsaxHandler.Application_End(sender, e); 
    } 

    public void Application_OnPreRequestHandlerExecute(object sender, EventArgs e) 
    { 
     Dynamicweb.Frontend.GlobalAsaxHandler.Application_OnPreRequestHandlerExecute(sender, e); 
    } 
} 

だから私は接続することができます。しかし、私はこのように、特定のリンクに行くことができない。 http://dynamicweb8724.nl/webapi/NavToDW/?process=

結果がこれです:

This XML file does not appear to have any style information associated with it. The document tree is shown below. 
<Error> 
<Message>An error has occurred.</Message> 
<ExceptionMessage> 
The object has not yet been initialized. Ensure that HttpConfiguration.EnsureInitialized() is called in the application's startup code after all other initialization code. 
</ExceptionMessage> 
<ExceptionType>System.InvalidOperationException</ExceptionType> 
<StackTrace> 
bij System.Web.Http.Routing.RouteCollectionRoute.get_SubRoutes() bij System.Web.Http.Routing.RouteCollectionRoute.GetRouteData(String virtualPathRoot, HttpRequestMessage request) bij System.Web.Http.WebHost.Routing.HttpWebRoute.GetRouteData(HttpContextBase httpContext) 
</StackTrace> 
</Error> 

コントローラー:

public class GuestbookApiControllerController : ApiController 
{ 
    // GET: GuestbookApiController 
    public IEnumerable<GuestbookEntry> Get() 
    { 
     return ItemManager.Storage.GetByParentPageId<GuestbookEntry>(1067); 
    } 
} 

だから私は変更する必要がありますでしょうか?

しかし、私は、このメソッドにブレークポイントを置く場合:

public void Application_Start(object sender, EventArgs e) 
     { 
      AreaRegistration.RegisterAllAreas(); 
      GlobalConfiguration.Configure(WebApiConfig.Register); 
      RouteConfig.RegisterRoutes(RouteTable.Routes); 

      ViewEngines.Engines.Clear(); 
      ViewEngines.Engines.Add(new Dynamicweb.AspNet.Views.RazorViewEngine()); 
      ViewEngines.Engines.Add(new Dynamicweb.AspNet.Views.WebFormViewEngine()); 
      // Fires when the application is started 

      //GlobalConfiguration.Configuration.MapHttpAttributeRoutes(); 
      GlobalAsaxHandler.Application_Start(sender, e); 
      GlobalConfiguration.Configuration.EnsureInitialized(); 
     } 

それがヒットdoesntの。

+0

あなたの 'ApiController'コードはどこですか? –

+0

申し訳ありませんが、ApiControllerはありません – InfinityGoesAround

+1

なぜ、あなたはそれを使用しない場合、Web APIを設定していますか? web api *内のコントローラはApiControllerから継承する必要があります。あなたのコードには多くの問題があり、あなたがその主題を知らない場合は対処できないので、Web Apiについてもっと読むことを強くお勧めします。 –

答えて

0

WebApiのルート設定を修正する必要があります。あなたは、テンプレート自体ではなく、ルート名にあなたが望むもののルートテンプレートを配置しました。意図されたURLを考えると

...

http://Dynamicweb8724.nl/webapi/NavToDW/?process=" 

あなたが作る必要があるいくつかの変更があります。

まず、パラメータprocessを受け入れることができるようにする必要があります。また、コントロールに従うようにコントローラの名前を変更します。

public class GuestbookApiController : ApiController 
{ 
    // GET: GuestbookApi 
    public IEnumerable<GuestbookEntry> Get(int process) 
    { 
     return ItemManager.Storage.GetByParentPageId<GuestbookEntry>(process); 
    } 
} 

最後に、意図したURLをコントローラアクションに適切にマッピングする必要があります。

public class WebApiConfig { 
    public static void Register(HttpConfiguration config) { 

     // Web API configuration and services 

     // Web API routes 
     config.MapHttpAttributeRoutes(); 

     // Convention-based routing. 

     //This will map to the intended controller 
     config.Routes.MapHttpRoute(
      name: "GuestBookApiRoute", 
      routeTemplate: "webapi/NavToDW", 
      defaults: new { controller = "GuestbookApi" } 
     ); 

     //This is the default api route 
     config.Routes.MapHttpRoute(
      name: "DefaultWebApi", 
      routeTemplate: "api/{controller}/{id}", 
      defaults: new { id = RouteParameter.Optional } 
     ); 

    }  
} 

上記は、コンベンショナルベースのルーティングを行う方法を示しています。属性ルーティングでも同じことができます。属性のルーティングをしたい場合は、時間をかけてトピックを読むことができます

+0

こんにちは、ありがとう。しかし、私はその投稿を編集する。 – InfinityGoesAround