有没有办法用WebAPI生成Urls?

时间:2015-11-13 08:41:34

标签: c# asp.net-mvc model-view-controller asp.net-web-api

以下编辑

我们无法弄清楚为什么UrlHelper在从WebApi控制器的上下文中使用时返回空字符串。

我们已经完成了必要的调试,但是我们无法找出发生这种情况的原因,RouteData中有路由,但它似乎并没有起作用。

在大多数情况下,我们使用RenderViewToString函数,该函数加载包含对Url.RouteUrl(routeName)的调用的视图。

尝试过的东西是创建自定义UrlHelper(但无济于事)并使用UrlHelper(MVC / HTTP)进行调试。

路由名称随处可用属性路由。

使用代码示例:

    public class WebApiController : BaseApiController
    {
        [HttpPost]
        [ResponseType(typeof(string))]
        [Route("cart/get/checkout", Name = "api.cart.get.checkout")]
        public IHttpActionResult GetCheckOutShoppingCart([FromBody] string data)
        {
               return Ok(RenderViewToString("CartController", "_CheckOutCartPartial", new ShoppingCartModel(Auth.IsAuthenticated ? Auth.GetCustomer().DefaultShippingInfo.CountryId : 148)
               {
                   AddInsurance = false,
                   InsuredShipping = insuredShipping,
                   CurrentDeliveryMethodId = deliveryMethodId,
                   CurrentPaymentMethodId = paymentMethodId
               }));
        }
   }

BaseApiController类:

 public class BaseApiController : ApiController
    {
        public static string RenderViewToString(string controllerName, string viewName)
        {
            return RenderViewToString(controllerName, viewName, new Dictionary<string, object>());
        }

        public static string RenderViewToString(string controllerName, string viewName, object model)
        {
            using (var writer = new StringWriter())
            {
                var routeData = new RouteData();
                routeData.Values.Add("controller", controllerName);
                var fakeControllerContext =
                    new ControllerContext(
                        new HttpContextWrapper(new HttpContext(new HttpRequest(null, "http://google.com", null),
                            new HttpResponse(null))), routeData, new FakeController());
                var razorViewEngine = new RazorViewEngine();
                var razorViewResult = razorViewEngine.FindView(fakeControllerContext, viewName, "", false);
                var viewContext = new ViewContext(fakeControllerContext, razorViewResult.View,
                    new ViewDataDictionary(model), new TempDataDictionary(), writer);
                razorViewResult.View.Render(viewContext, writer);
                return writer.ToString();
            }
        }

        public static string RenderViewToString(string controllerName, string viewName, Dictionary<string, Object> data)
        {
            using (var writer = new StringWriter())
            {
                var viewData = new ViewDataDictionary();
                foreach (var kv in data)
                {
                    viewData[kv.Key] = kv.Value;
                }

                var routeData = new RouteData();
                routeData.Values.Add("controller", controllerName);
                var fakeControllerContext =
                    new ControllerContext(
                        new HttpContextWrapper(new HttpContext(new HttpRequest(null, "http://google.com", null),
                            new HttpResponse(null))), routeData, new FakeController());
                var razorViewEngine = new RazorViewEngine();
                var razorViewResult = razorViewEngine.FindView(fakeControllerContext, viewName, "", false);
                var viewContext = new ViewContext(fakeControllerContext, razorViewResult.View, viewData,
                    new TempDataDictionary(), writer);
                razorViewResult.View.Render(viewContext, writer);
                return writer.ToString();
            }
        }

        private class FakeController : ControllerBase
        {
            protected override void ExecuteCore()
            {
            }
        }
    }

修改

我们已经组建了一个理论上应该工作的课程,但它并没有。 RouteData在集合中具有MVC和API路由。

 public static class Url
    {
        public static bool IsWebApiRequest()
        {
            return
                HttpContext.Current.Request.RequestContext.HttpContext.CurrentHandler is
                    System.Web.Http.WebHost.HttpControllerHandler;
        }

        public static string RouteUrl(string routeName, object routeValues = null)
        {
            var url = String.Empty;
            try
            {
                if (IsWebApiRequest())
                {
                    var helper = new System.Web.Http.Routing.UrlHelper();
                    url = helper.Link(routeName, routeValues);             
                }
                else
                {
                    var helper = new System.Web.Mvc.UrlHelper();
                    url = helper.RouteUrl(routeName, routeValues);              
                }

                return url;
            }
            catch
            {
                return url;
            }
        }

        public static string HttpRouteUrl(string routeName, object routeValues = null)
        {
            var url = String.Empty;
            try
            {
                if (IsWebApiRequest())
                {
                    var helper = new System.Web.Http.Routing.UrlHelper();
                    url = helper.Link(routeName, routeValues);
                }
                else
                {
                    var helper = new System.Web.Mvc.UrlHelper();
                    url = helper.HttpRouteUrl(routeName, routeValues);
                }

                return url;
            }
            catch
            {
                return url;
            }
        }
    }

4 个答案:

答案 0 :(得分:3)

此问题已通过构建自定义 UrlHelper 类来解决,该类会查看RouteTable,然后返回更换了模式的网址。

public static class Link
{
    public static string RouteUrl(string routeName, object routeValues = null)
    {
        var url = String.Empty;
        try
        {
            var route = (Route)RouteTable.Routes[routeName];
            if (route == null)
                return url;

            url = "~/".AbsoluteUrl() + route.Url;
            url = url.Replace("{culture}", System.Threading.Thread.CurrentThread.CurrentCulture.TwoLetterISOLanguageName.ToLower());

            if (routeValues == null) 
                return url;

            var values =  routeValues.GetType().GetProperties();
            Array.ForEach(values, pi => url = Regex.Replace(url, "{" + pi.Name + "}", pi.GetValue(routeValues, null).ToString()));

            return url;
        }
        catch
        {
            var newUrl = RouteUrl("403");
            if(newUrl == String.Empty)
                throw;

            return newUrl;
        }
    }

    public static string HttpRouteUrl(string routeName, object routeValues = null)
    {
       return RouteUrl(routeName, routeValues);
    }
}

答案 1 :(得分:1)

尝试Uri.Link(routeName,object)

var uri = Url.Link("api.cart.get.checkout", new { id = 1 });

如果属性名称与路由参数匹配,则会将给定对象的属性注入路径。

答案 2 :(得分:1)

WebApi不是MVC。即使它看起来非常相似,但它是一个完全不同的系统。

UrlHelper(它是MVC)将查看MVC路由表,并忽略任何WebApi路由。

尝试以艰难的方式创建路径,基本上将控制器和操作名称硬编码到视图中。 : - (

答案 3 :(得分:1)

好的,根据您的评论,您似乎试图从MVC layout.cshtml 文件中调用Url.RouteUrl(string routeName, object routeValues = null)Url.HttpRouteUrl(string routeName, object routeValues = null)

如果是这种情况,Url.IsWebApiRequest()将返回false,因为 layout.cshtml 文件仅作为处理MVC请求而非WebAPI的一部分进行处理。这将导致url生成方法使用MVC路由集合而不是WebAPI集合。

更改您的Url类,以便RouteUrl始终构建WebAPI网址,HttpRouteUrl只构建MVC网址然后在您的布局文件中使用适当的方法,根据您需要的网址类型具体情况。

public static class Url
{
    public static bool IsWebApiRequest()
    {
        return
            HttpContext.Current.Request.RequestContext.HttpContext.CurrentHandler is
                System.Web.Http.WebHost.HttpControllerHandler;
    }

    public static string RouteUrl(string routeName, object routeValues = null)
    {
        var url = String.Empty;
        try
        {
            var helper = new System.Web.Http.Routing.UrlHelper();
            return helper.Link(routeName, routeValues);             
        }
        catch
        {
            return url;
        }
    }

    public static string HttpRouteUrl(string routeName, object routeValues = null)
    {
        var url = String.Empty;
        try
        {
            var helper = new System.Web.Mvc.UrlHelper();
            return helper.HttpRouteUrl(routeName, routeValues);
        }
        catch
        {
            return url;
        }
    }
}