Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7606765
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:29:30+00:00 2026-05-31T00:29:30+00:00

I am developing some REST resources using the ASP.NET MVC 3 platform. So far,

  • 0

I am developing some REST resources using the ASP.NET MVC 3 platform. So far, its been great, I love the flexibility of MVC 3, and it is a breeze to build REST services with it. One area where I have been having a difficult time are the route constraints in Global.asax. For one, they never seem to work properly (including one ALWAYS returns 404, even if the input most definitely meets the requirements of the constraint…but thats another question).

Second, and more importantly, the result returned when a constraint fails is always an HTML page. Assuming the constraints work, having the result be HTML really throws a wrench in the mix when all of the consumers of these REST services will be accepting data types, such as application/json, text/xml, possibly bson, etc. I really need to be able to address errors in our clients directly, rather than simply blowing up and logging the html for sideband resolution.

Is it possible to change what gets returned in response to a route constraint? Is it possible to make whats returned dynamic, such that if the client issuing the request only accepts application/bson, we can generate an acceptable response, rather than simply generating a canned response of a single mime type?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T00:29:31+00:00Added an answer on May 31, 2026 at 12:29 am

    About returning a code error instead of going to a controller you have to implemet a custom RouteHandler. This link resumes all thing you can put the finger on …and that you might modify this way.
    About “adapting” the return type…you can do this in the controller. It is enough to put som if and in some cases you return Json(…) and in other cases you return View or PartialView.

    However it is nice to do this in a controller filter…!

    I implemented a Control filter that allows the controller to negotiate the return type with the client. I is very simple…the client just declare the type that i would like to receive either in a route parameter (a piece of the url, or of the query string) or by posting a field that contains this info.
    The use is very simple…you rcontroller can return a View thai the “default” return type if no infos come from the client. Then the filter “change” automatically the result type before the View is invoked transformig it in what the client required. The possibilties handled by the filter are a view whose name is chosen by the client or Json

    The code is here(It contains some controls on the “hint” provided by the client to prevent attacks by malicious users):

    public class AcceptViewHintAttribute : ActionFilterAttribute
    {
        private JsonRequestBehavior jsBehavior;
        public AcceptViewHintAttribute(JsonRequestBehavior jsBehavior = JsonRequestBehavior.DenyGet)
        {
            this.jsBehavior = jsBehavior;
        }
        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            string hint = filterContext.RequestContext.HttpContext.Request.Params["ViewHint"];
            if (hint == null) hint = filterContext.RequestContext.RouteData.Values["ViewHint"] as string;
            if (!string.IsNullOrWhiteSpace(hint) && hint.Length<=100 && new Regex(@"^\w+$").IsMatch(hint) )
            {
    
    
                    ViewResultBase res = filterContext.Result as ViewResultBase;
                    if (res != null)
                    {
                        if (hint == "json")
                        {
                            JsonResult jr = new JsonResult();
                            jr.Data = res.ViewData.Model;
                            jr.JsonRequestBehavior = jsBehavior;
                            filterContext.Result = jr;
                        }
                        else
                        {
                            res.ViewName = hint;
                        }
                    }
    
            }
            base.OnActionExecuted(filterContext);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Background We are developing some in-house utilities using ASP.NET 2.0. One of which is
I would like to do some Office Interop stuff using .NET 4.0, but developing
I've been developing traditional ASP.NET applications with server side ASP.NET controls and code behind
I'm developing a client/server app that will communicate via rest. Some custom request data
I'm developing some cross platform software targeting Mono under Visual Studio and would like
I am developing some client side Javascript that is using some JSON web services
I am developing some program in C# which will send the mail using outlook
So, I'm developing a REST webservice using RESTeasy and Google App Engine. My question
I've been implementing a GWT application that calls a REST-service (which we're also developing).
I'm developing a Classic ASP page that pulls some content from a database and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.