Using codec file extensions with OpenRasta returns

2020-03-05 23:59发布

When using codec uri file extensions with OpenRasta, OR can't resolve the uri and returns a 404. Without the file extension all works ok.

The codecs are defined for the object resource and I'm using both XmlDataContract and JsonDataContract. Using neither the .xml or .json extension works, this is for both InMemoryHost (which we're using for testing) and ASP.Net (IIS7, integrated mode).

Codec configuration:

ResourceSpace.Has.ResourcesOfType<object>()
                .WithoutUri
                .AsXmlDataContract()
                .And.AsJsonDataContract();

Is there anything else that needs to be done to make uri file extensions work?

标签: openrasta
1条回答
ら.Afraid
2楼-- · 2020-03-06 00:20

You need to register the ContentTypeExtensionUriDecorator as a UriDecorator in OpenRasta in order to expose the .xml, .json functionallity.

The below example should allow you to make http requests to:

GET /home.json

GET /home.xml

public class RastaConfig : IConfigurationSource
{
    public void Configure()
    {
        using(OpenRastaConfiguration.Manual)
        {
            ResourceSpace.Uses.UriDecorator<ContentTypeExtensionUriDecorator>();

            ResourceSpace.Has.ResourceOfType<Home>()
                .AtUri("/home")
                .HandledBy<HomeHandler>()
                .AsXmlDataContract()
                .And.AsJsonDataContract();
        }
    }
}

This is because noramlly the client will add an HTTP Accept header to define the content types it supports and is interested in.

For more information you can read about Content Negotiation (often referred to as conneg) on the web.

OpenRasta will then select the return content type based on the client 's preference in the HTTP Accept header.

Hope this helps.

查看更多
登录 后发表回答