Posts Tagged ‘ASP.NET MVC’

Creating an URL to a WebAPI ApiController inside an ASP.NET MVC View

March 27, 2012


Today I was trying to add some dynamic content to my new IT consulting website. My website is based on ASP.NET MVC, but I needed to load some dynamic content in one part of the page and I thought I would try out the new ASP.NET WebAPI framework for this.


Creating the ApiController was easy, but when I wanted to use the controller in my ASP.NET MVC view, I wasn’t sure how to include the Url of the controller. Hardcoding the address didn’t feel right, especially because for the MVC controllers you have the really useful Url.Action method, which takes the name of an action and a controller and generates an URL to call this action. Unfortunately there wasn’t any built in method to call an ApiController. My first attempt was to use the following call instead, specifying the name of the route I want to use directly:

Url.RouteUrl("DefaultApi", new{controller="[CONTROLLERNAME]"})

Unfortunately, this didn’t work as expected — the function didn’t produce any Url.

After some googling I came across this stackoverflow post, with the missing piece of the puzzle:


Basically all you have to do is to add an empty httproute parameter to the Url.RouteUrl call. This apparently helps the system differentiate between normal routes and WebApi routes. So the final call looks like this:

Url.RouteUrl("DefaultApi", new{httproute = "", controller="[CONTROLLERNAME]"})

Keep in mind that the first parameter to RouteUrl refers to the name of the route you used in the RegisterRoutes function, which is “DefaultApi” by default.

Akzente.IT – New web site online!

March 22, 2012

My new web site at akzente.IT is online!

You will find some more information about me and my projects there.

My goals for the web page were twofold. First of all I wanted to have a nice virtual “business card” – a place where I can present myself to potential employers with a list of my skills and the projects I have done in the past.

My second goal was to use this opportunity to learn more about ASP .NET MVC (with Razor) and HTML5 and I have to say I’m very happy with the results. I think it was the first time I actually enjoyed creating a website. I have full control of the generated HTML, I didn’t have any hard to debug errors so far and the separation that MVC gives me is really cool. I’m actually using this to generate different views of my project page. I have a single controller responsible for the list of projects which delegates to different views depending on an input parameter (e.g. to generate a text only version for carreer networks) and it works quite nice. I’m thinking about extending this to automatically create a resumé in Word generating Office Open XML with a Razor view. I’m curious if this works.

Another interesting page is the blog page where I show some recent blog posts with links back to this blog. It was amazing how easy it was to build this.

public class BlogController : Controller
    [OutputCache(Duration = 60*60, VaryByParam = "none")]
    public ActionResult Index()
        var model = new Blog();
        string feedUrl = "";
        using (XmlReader reader = XmlReader.Create(feedUrl))
            var feed = SyndicationFeed.Load(reader);
            model.Feed = feed;
        return View(model);

My favourite is the controller attribute for output caching, which will cache the generated result for 1 hour, so that it loads faster and doesn’t hit the blog to hard in case there are many visitors.

That’s all about the website for now. I also have a profile now on GULP, check it out: