【问题标题】:url of webapi, how to configure in mvcweb api的url,如何在mvc中配置
【发布时间】:2016-05-18 11:18:30
【问题描述】:

我有一个 webapi 的 url,如下所示:

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

在 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 }
        );

    }    
}

以及 Global.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();
        }

它没有击中。

【问题讨论】:

  • 您的ApiController 代码在哪里?
  • 对不起,没有ApiController
  • 不使用web api为什么还要配置呢? Web api 中的任何控制器必须 继承自 ApiController。我强烈建议您阅读有关 Web Api 的更多信息,因为您的代码存在很多问题,如果您不了解主题,则无法解决。

标签: asp.net-mvc-3 asp.net-mvc-4 asp.net-web-api routing global-asax


【解决方案1】:

您的 WebApi 路由配置需要修复。您将所需的路由模板放入路由名称而不是模板本身。

鉴于预期的网址...

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

您需要进行一些更改。

首先,您的 api 控制器需要能够接受参数 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 }
        );

    }    
}

上面展示了如何进行基于约定的路由。您也可以通过属性路由实现相同的目的。如果你想做属性路由,你可以花一些时间阅读主题

【讨论】:

    猜你喜欢
    • 2012-03-11
    • 2017-03-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多