<h2>Problem</h2>

<p>My current project requires me to do different things based on different HTTP request headers for nearly every action.</p>

<p>Currently, I have one massive Controller (all for the same resource type), and every action method has an ActionName attribute (so that I can have multiple versions of the same action that takes the same parameters, but does different things) and a custom FilterAttribute (implemented almost exactly like the AcceptVerbsAttribute in Preview 5) that checks if certain headers have certain values.</p>

<p>I would really like to push the code into separate Controllers, and have the RouteTable select between them based on the headers, but can't think of the cleanest way to do this.</p>

<h2>Example</h2>

<p>For example, say I have a list of files.  The service must process the request in one of two ways:</p>

<ol>
<li><p>The client wants a zip file, and passes "accept: application/zip" as a header, I take the list of files, pack them into a zip file, and send it back to the client.</p></li>
<li><p>The client wants an html page, so it passes "accept: text/html", the site sends back a table-formatted html page listing the files.</p></li>
</ol>

[{'answer_id': 78864, 'author': 'Aeon', 'author_id': 13289, 'author_profile': 'https://Stackoverflow.com/users/13289', 'pm_score': 0, 'selected': False, 'text': "<p>I'm not sure you need separate controllers based on header; this structure sounds perfectly reasonable. If your controller is massive as you say, consider whether it's dealing with multiple resources, and if it is, perhaps it should be split into multiple controllers based on resource?</p>\n"}, {'answer_id': 78872, 'author': 'Bryant', 'author_id': 10893, 'author_profile': 'https://Stackoverflow.com/users/10893', 'pm_score': 0, 'selected': False, 'text': '<p>Not sure if it is possible, but it seems like this would be something like the AcceptVerbs attribute that was <a href="http://weblogs.asp.net/scottgu/archive/2008/09/02/asp-net-mvc-preview-5-and-form-posting-scenarios.aspx" rel="nofollow noreferrer">added in Preview 5</a>. I\'d take a look at how that was implemented (get the MVC source) to see if you can add something similar based on content type. </p>\n'}, {'answer_id': 84718, 'author': 'Haacked', 'author_id': 598, 'author_profile': 'https://Stackoverflow.com/users/598', 'pm_score': 3, 'selected': True, 'text': '<p>It sounds like you have slightly different behavior from your actions based on which header comes in. I would try to isolate the differences as much as possible. </p>\n\n<p>For example, if the application logic is the same, but the only difference is how you render the response to the user, you might consider writing a custom ActionResult that takes different actions based on the Http headers.</p>\n\n<p>However, if the logic is completely different, you could implement a custom Routing constraint (IRoutConstraint) that you attach to each route. Take a look at the implementation of HttpMethodConstraint for ideas.</p>\n'}, {'answer_id': 645186, 'author': 'zihotki', 'author_id': 66591, 'author_profile': 'https://Stackoverflow.com/users/66591', 'pm_score': 0, 'selected': False, 'text': '<p>You should to look at <a href="http://omaralzabir.com/create_rest_api_using_asp_net_mvc_that_speaks_both_json_and_plain_xml/" rel="nofollow noreferrer">this post</a>. It describes implementation for json and xml responses based on http header.</p>\n'}]
['https://Stackoverflow.com/questions/78823', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/5897/']