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 962295
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T01:29:03+00:00 2026-05-16T01:29:03+00:00

I’m trying to write REST-behaviour into my ASP.NET MVC2 app, but I’m having a

  • 0

I’m trying to write REST-behaviour into my ASP.NET MVC2 app, but I’m having a hard time figuring out how to make the routes work as I want.

I’d like my routing to work like this:

/Users/Get/1 <- returns a regular HTML-based reply
/Users/Get.xml/1 <- returns the data from Get as XML
/Users/Get.json/1 <- returns the data as JSon

I’ve tried setting up routes like this:

routes.MapRoute("Rest", 
 "{controller}/{action}{format}/{id}" (...)

But it complains I need a separator between {action} and {format}

also the following:

routes.MapRoute("Rest",
     "{controller}/{action}.{format}/{id}" (...)

makes the /Users/Get/1 invalid (it needs to be /Users/Get./1 which is unacceptable)

Any suggestions?

————-EDIT————————————

I have one solution right now, but I’m not really happy with it:

routes.MapRoute(
            "DefaultWithFormat", // Route name
            "{controller}/{action}.{format}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", format = "HTML", id = UrlParameter.Optional } // Parameter defaults
        );
        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );

This works with both /Users/Get.whateverFormat/1 and also /Users/Get/1

Reason for this is that when I just do /Users/Get/1 (without the .format) it skips the first route, and goes to the next which doesn’t include a format.
To handle the return I’ve created an ActionFilterAttribute and override the OnActionExecuted method like this:

var type = filterContext.RouteData.Values["format"];
if (type != null && attributes != null)
{
    if (type == "HTML") return;
    if (type.ToString().ToLower() == "xml" && attributes.Any(a => a.AllowedTypes.Any(a2 => a2 == ResponseType.XML)))
    {
        filterContext.Result = new XmlResult(filterContext.Controller.ViewData.Model);
        filterContext.HttpContext.Response.Clear();
        filterContext.HttpContext.Response.ContentType = "text/xml";
        return;
    }
    if (type.ToString().ToLower() == "json" && attributes.Any(a => a.AllowedTypes.Any(a2 => a2 == ResponseType.JSON)))
    {
        filterContext.Result = new JsonResult() { Data = (filterContext.Controller.ViewData.Model), JsonRequestBehavior = JsonRequestBehavior.AllowGet };
        filterContext.HttpContext.Response.Clear();
        filterContext.HttpContext.Response.ContentType = "text/json";
        return;
    }
}

And I also have a ResponseTypeAttribute which allows me to decorate the actions with what returntype they should allow:

[AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = true)]
public sealed class ResponseTypeAttribute : Attribute
{
    List<ResponseType> allowedTypes;

    public List<ResponseType> AllowedTypes
    {
        get { return allowedTypes; }
        set { allowedTypes = value; }
    }

    public ResponseTypeAttribute(params ResponseType[] allowedTypes)
    {
        this.allowedTypes = new List<ResponseType>();
        this.allowedTypes.AddRange(allowedTypes);
    }


}

public enum ResponseType
{
    XML, JSON
}

The XmlResult is just a simple object serializer.

  • 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-16T01:29:04+00:00Added an answer on May 16, 2026 at 1:29 am

    Brad Wilson made a talk with title Advanced ASP.NET MVC2 where he has shown an example how to do exactly what you want. You can download the slides and example code here:

    http://bradwilson.typepad.com/blog/talks.html

    (It is the first talk on the page, and if I remember well restful urls is the first topic in the talk.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.