Stephen Brannan - 162 - Dec 21, 2013

Is there a way to have a rest path with /api for json and then for html just / ? Using the razor rockstar project as an example… If I’m viewing the page as html then it’s /rockstars but if it’s a json request then I want to force them to use /api/rockstars.

Other than explicitly specifying routes like [Route("/api/hello")] there’s no difference between json or html routes.

Stephen Brannan:

What about a way to disallow HTML output for some services and allow it for others?

Just create a request attribute filter that detects and short-circuits Html requests (i.e. IHttpRequest.ResponseContentType), then you can apply the attribute to the selected services.

Stephen Brannan:

Thanks. I was considering that approach. Do you see any value in adding a FormatAttribute to ServiceStack? If so, I might be able to find the time to contribute that addition.

There’s not been any demand for it, what’s it intended to do?

Stephen Brannan:

Allow you to control the ResponseContentType on a service by service basis. For example if I put in Format[(Json | Xml)] then the service can only accept and respond to those two content types.

That would duplicate functionality with the Restrict Attribute: https://github.com/ServiceStack/ServiceStack/wiki/Restricting-Services

Stephen Brannan:

ah hah! That was what I was looking for. perfect. I don’t have to write this part after all. :slight_smile: I didn’t realize Restrict was able to limit the format too.