如何在WebApiConfig.cs中使用不同的控制器

时间:2017-06-19 16:50:25

标签: c# asp.net web asp.net-web-api2

我试图在我的WebApiConfig中使用更模块化的设计,我可以在其中设置路由并引用其他控制器。但是,我似乎无法找到真正有用的东西。

using System;
using System.Net.Http.Formatting;
using System.Web.Http;
using ASCI.ConstantsStrorage;

namespace DashBoard.API
{
public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {

        // Web API routes
        config.MapHttpAttributeRoutes();
        // Routing for email service 
        config.Routes.MapHttpRoute(
            name: "EmailApiRoute",
            routeTemplate: Constants.BaseEndPointV1 + "{controller}",
            defaults: new { controller = "ASCIEmailController" }
        );
        //routing for accessing different fields 
        config.Routes.MapHttpRoute(
                name: "FieldsApiRoute",
                routeTemplate: Constants.BaseEndPointV1 + "{controller}/{name}/field/{fieldName}/{data}",
                defaults: new{
                        name = RouteParameter.Optional,
                        fieldName = RouteParameter.Optional,
                        data = RouteParameter.Optional
                }
        );          
        //default routing for raw dashboard objects  
        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: Constants.BaseEndPointV1 + "{controller}/{name}",
            defaults: new { name = RouteParameter.Optional }
        );
        // allows for json to be the default response type, but still allows you to specify different response types (i.e XML) 
        GlobalConfiguration.Configuration.Formatters.JsonFormatter.MediaTypeMappings
        .Add(new RequestHeaderMapping("Accept",
                            "text/html",
                            StringComparison.InvariantCultureIgnoreCase,
                            true,
                            "application/json")
            );
    }
}
}

正如您所见,我尝试将控制器设置为下面的其他控制器。

using System.Collections.Generic;
using System.Web.Http;
using System.Web.Http.Description;
using ASCI.ETW_Tracing;
using ASCI.ConstantsStrorage;

namespace DashBoard.API.Controllers
{
    public class ASCIEmailController : ApiController
    {
        [HttpPost]
        [Route(Constants.BaseEndPointV1 + "email")]
        [ResponseType(typeof(Models.Email))]
        public IHttpActionResult PostEmail([FromBody]Models.Email email)
        {
            // allows us to send the email
            EmailHelper emailHelper = new EmailHelper();
            Models.Email sentEmail = emailHelper.SendEmail(email);

            return CreatedAtRoute("EmailApiRoute", new { id = email.Body }, sentEmail);
        }
    }
}

我无法找到解决此问题的方法,非常感谢任何帮助。

namespace ASCI.ConstantsStrorage
{
    public static class Constants
    {
        /// <summary>
        /// Constant that is used throughout the API for version control.
        /// Currently on version 1.0 of the API
        /// </summary>
       public const string BaseEndPointV1 = "api/v1/";
    }
}

0 个答案:

没有答案