Click here to Skip to main content
15,870,297 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
Hey friends!

I'm writing an API like ASP.NET MVC application. I want users to decide how the response data is formatted by a call to the MVC app with an file extension like route.

For example :
http://base.url/controller/action.xml
Should perform some kind of action and return data in XML format

http://base.url/controller/action.json
Should perform the exact same action as the xml version, only the response data is formatted using Json.

I've added a rout in the Global.asax :
C#
routes.MapRoute("ResponseExtension",
    "{controller}/{action}.{datatype}",
    new {
        controller = "Home",
        action = "Index",
        datatype = UrlParameter.Optional 
    });


To allow the period in the URL, i've added the line <add key="ClientValidationEnabled" value="true"/> to my web.config in the <system.web> section. I even found some article on the web which told me I shout remve the extension in the web.config like so :
XML
<buildProviders>
    <remove extension=".xml"/>
    <remove extension=".json"/>
</buildProviders>


Still, after som playing and trying to get it to work, still the same error :
The resource cannot be found
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.

Now the big question is : How do I get it to work?
FYI : Working with MVC 3 with C#.NET 4.0

Thanks in advance!
Eduard
Posted

1 solution

I was able to achieve something like this. Check it out:

First, I created a controller for testing this:

public class RoutesController : Controller
    {
        //
        // GET: /Routes/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult About()
        {
            return View();
        }
    }


In one of the existing views (apart from the 2 views below) I added this:

<%= Html.RouteLink("urls", "Custom") %>


Then a Index and About view under Views/Routes as shown below:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Index
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <h2>Index</h2>

    index.mvc

    <br />

    <%= Html.RouteLink("more","ResponseExtension", new { datatype = "json" }) %>

</asp:Content>

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    About
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <h2>About</h2>

</asp:Content>


Then add the following to your RegisterRoutes:

routes.MapRoute("Custom", "routes/index.mvc", new { controller = "Routes", action = "Index" });

            routes.MapRoute("ResponseExtension",
                            "testpg/about.{datatype}",
                            new
                            {
                                controller = "Routes",
                                action = "About",
                                datatype = @"\[a-z]+"
                            });


It worked fine for me! Give it a try!!

EDIT - If I changed testpg/about.{datatype} to testpg/{action}.{datatype} it didn't work though. I will try that out too when I find some time!
 
Share this answer
 
v4
Comments
Karthik. A 24-Jun-11 15:58pm    
You could improvise the About() method as About(string datatype)
{
return JsonResult(..); //or whatever you require
}
Eduard Keilholz 25-Jun-11 8:49am    
Thanks for your reply man, i'll give it a testrun on monday

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900