如何使用ASP.NET MVC Web API OData链接到Razor中的OData集合 [英] How to Link to OData Collection in Razor using ASP.NET MVC Web API OData

查看:51
本文介绍了如何使用ASP.NET MVC Web API OData链接到Razor中的OData集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ASP.NET MVC 4应用程序,正在将OData API集成到其中.它正在运行具有更大OData支持的2012.2.

我没有为此使用一个单独的区域...这可能是一个错误,但是我的应用很小,该区域似乎过大了.

我已经正确设置了控制器,并且我的Segments集合(segments是我的域中的一种类型)的示例路径是"/odata/Segments".这将按预期加载并正常运行.

在我的主页上,我试图使用Razor的Html.ActionLink(或RouteLink)添加到此资源的链接,但是OData控制器布局似乎不适用于这些方法,因为控制器的前缀是"odata"在WebAPIConfig中注册时:

config.Routes.MapODataRoute("OData Route", "odata", model );

我可以通过假装有一个odata控制器(当然,据我所知)没有这样的东西来欺骗构造正确的url的方法:

@Html.RouteLink("Segments", "Segments", "odata")

但这似乎是一种黑客.

我不太了解ASP.NET路由管道,以了解传递给MapODataRoute的前缀如何被合并到MVC链中,这样我就可以以正确"的方式使用正确"的剃须刀方法. /p>

只为踢球,这是我的SegmentsController:

public class SegmentsController : EntitySetController<Segment, long>
{

    private MarketerDB db = new MarketerDB();

    // GET api/segments
    override public IQueryable<Segment> Get()
    {
        return db.Segments.AsQueryable();
    }


    protected override Segment GetEntityByKey(long key)
    {
        return db.Segments.Find(key);
    }


    public IQueryable<Affiliate> GetAffiliates([FromODataUri] long key)
    {
        return this.GetEntityByKey(key).Affiliates.AsQueryable();
    }

    protected override void Dispose(bool disposing)
    {
        db.Dispose();
        base.Dispose(disposing);
    }
}

解决方案

我们有一个

确保在剃刀视图中具有@using System.Web.Http.OData.Routing指令.

I have an ASP.NET MVC 4 app that i'm incorporating an OData API into. This is running the 2012.2 stuff with the larger OData support.

I did not use a separate area for this...that might have been a mistake but my app is small and area seemed overkill.

I've got my controllers setup correctly and an example path to my Segments collection (segments is a type in my domain) is "/odata/Segments". This loads as expected and is working.

On my homepage i'm trying to add a link to this resource using Razor's Html.ActionLink (or RouteLink) but it seems the OData controllers layout doesn't quite work with those methods because the controllers are prefixed with "odata" when registered in WebAPIConfig:

config.Routes.MapODataRoute("OData Route", "odata", model );

I can trick the method to construct the correct url by pretending there's an odata controller when there certainly isn't one (as far as i know) with something like this:

@Html.RouteLink("Segments", "Segments", "odata")

but that seems like a hack.

I don't quite understand the ASP.NET routing plumbing well enough to understand how that prefix passed to MapODataRoute is being incorporated into the MVC chain so that i can use the "right" razor method the "right" way.

just for kicks, here's my SegmentsController:

public class SegmentsController : EntitySetController<Segment, long>
{

    private MarketerDB db = new MarketerDB();

    // GET api/segments
    override public IQueryable<Segment> Get()
    {
        return db.Segments.AsQueryable();
    }


    protected override Segment GetEntityByKey(long key)
    {
        return db.Segments.Find(key);
    }


    public IQueryable<Affiliate> GetAffiliates([FromODataUri] long key)
    {
        return this.GetEntityByKey(key).Affiliates.AsQueryable();
    }

    protected override void Dispose(bool disposing)
    {
        db.Dispose();
        base.Dispose(disposing);
    }
}

解决方案

We have an ODataLink method on System.Web.Http.UrlHelper but we forgot to add one to the MVC System.Web.Mvc.UrlHelper. Till we add it, you can use this extension method,

namespace System.Web.Mvc
{
    public static class UrlHelperExtensions
    {
        private static IODataPathHandler _pathHandler = new DefaultODataPathHandler();

        public static string ODataUrl(this UrlHelper urlHelper, string routeName, params ODataPathSegment[] segments)
        {
            string odataPath = _pathHandler.Link(new ODataPath(segments));
            return urlHelper.HttpRouteUrl(
                routeName,
                new RouteValueDictionary() { { ODataRouteConstants.ODataPath, odataPath } });
        }
    }
}

and call it from your razor views by doing something like (assuming there is an entityset customers and you want to put the navigation link to orders on customers(42)),

@Url.ODataUrl("odata", new EntitySetPathSegment("customers"), new KeyValuePathSegment("42"), new NavigationPathSegment("orders"))

Make sure you have an @using System.Web.Http.OData.Routing directive in your razor view.

这篇关于如何使用ASP.NET MVC Web API OData链接到Razor中的OData集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆