Saturday, 16 July 2011

Asp.net MVC 3 / Razor – Using the same view through regular action and Ajax loading

Ever had a view which you wanted to reuse and load through a regular action call or through using the action using a JavaScript call but then it loads in the associated layout multiple times? I had this exact issue recently.

And the solution; in the _ViewStart.cshtml file which specifies the default layout replace:

@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}

with

@{
    Layout = Request.IsAjaxRequest() ? null : "~/Views/Shared/_Layout.cshtml";
}

Then in the controllers actions they still continue to return the same view but don’t have any layout associated to it. This could be used to add a specific “ajax” layout instead of none.

Sample project can be found on github.

Saturday, 28 May 2011

Asp.net Mvc 3, Razor and RenderAction

When playing with the Razor syntax for view creation in Asp.net MVC you need to remember its not quite a direct translation between it and the web forms view engine and it is a different syntax. I was frustrated by it earlier in the week and without internet connection continued couldn’t resolve it instantly. It comes down to the syntactic sugar which Razor gives you.

When it comes to methods on the Html helper you can’t use the syntactic sugar by itself you do need to give Razor some parentheses.

Web forms view engine:

<% Html.RenderAction("About"); %>

Razor view engine:

@{ Html.RenderAction("About"); }

not @Html.RenderAction("About")

Thursday, 26 May 2011

GotConnection v0.2

There are a few changes and tweaks from v0.1 in this release. It was after a mini code review by my mate Will who had some suggestions on the connectivity testing which I have incorporated. I’ve also updated the way the options can be passed in to closer match the original way I wanted it to work.

Update to connectivity testing

Instead of specifying if you are connected or not this release has been updated to try and connect automatically and if no connection is available then use the in development settings. When you go live this can be turned off so if there is an issue with Twitter in a production environment it will return an empty string. This will need to tested for in the consuming code but a small price to pay for testing connectivity. This is driven by a configuration switch:

<add key="GotConnection.Twitter.InDevelopment" value="true" />

While testing the code the request for data from Twitter is relatively speedy so putting this functionality into a try/catch didn’t feel like a bad thing.

Update usage

The original plan was to allow the options to be specified with an anonymous object in much the same way in which you can override defaults in jQuery functions by passing in a json object. This release has now been updated to allow for this for the same options as in v0.1. I will look to expand to the rest of the Twitter API Timeline options in later releases.

Using default options:

GotConnection.ITwitter twitter = GotConnection.ConnectTo.Twitter();
var result = twitter.TimeLine("WestDiscGolf");

Specify options:

GotConnection.ITwitter twitter = GotConnection.ConnectTo.Twitter();
var result = twitter.TimeLine("WestDiscGolf", new { count = 5 });

Valid options:

format = Defines the format in which you want the result to return. The limitation is still currently only json for offline development; default = json.

count = Defines the number of status records to return in the call; default = 5

include_rts = Specifies if native retweets should be returned in the number of status updates returned; default = true.

Released

The updated code can be found on github. As always any questions or suggestions then please let me know. Also if anyone does find this useful then please let me know if you’re using it and what for.

Monday, 23 May 2011

GotConnection v0.1

As crazy as it sounds with the amount of connectivity out in the wild these days there are still times when you need to do web based development when you’re not connected to the internet. This in itself isn’t usually an issue except for when you’re developing some web service integrations eg. a Twitter feed integration in a website and have no way of accessing the Twitter API. This has been the main inspiration behind this little project as I’m disconnected from the outside world for a couple of hours a day on my commute to and from work and sometimes I want to integrate with Twitter.

So I present GotConnection v0.1.

“How do I use it?”

The concept is pretty simple. You include the library into your asp.net web forms or asp.net MVC or any .net project and where you would write the code to create the web request to extract out the json from Twitter service server side you do this instead …

ITwitter twitter = GotConnection.ConnectTo.Twitter(new { option = “blah” });
var result = twitter.TimeLine(“WestDiscGolf”, 3);

The result returned is the json representation of the timeline data for WestDiscGolf and the last 3 tweets. Simples.

The interesting part comes when you are working offline and have no access to the internet. To continue to develop against the returned json data you don’t need to do any testing in code, or change any of your consuming code at all. All you need to do is update an application setting in the projects web.config file to say that the component isn’t online anymore. And that’s it.

Limitations

The only feature at the moment is the ability to get a users timeline in json format from the Twitter API. This is mainly because this is the only functionality that I require at the moment. The offline data understands the number of tweets to return, but in this version that’s it.

Future versions

I’d like to expand this project to allow for more options in the Twitter time line functionality, including support for the different formats it can return. I’d like to expand it out to other services which have read only data feeds such as blogger, facebook, generic rss/atom feeds etc. Future versions will use the Options class further. The idea behind the Options class is to allow for passing in options/defaults through as an anonymous object much like jquery defaults are set.

I’m not interested in making this a component which becomes a library to log in/out, post etc. to services such as Twitter as there are many out there already. This is purely there for developing against a read only service when you’re not online.

Where can I get it?

Well it is up on github. This is the first time I’ve uploaded any of my code to github so feel free to look around, fork the code and change it all, add in extras etc. If you do find this useful and add any extras in then please let me know and I’ll see about adding them into the main fork. Probably in the next version or 2 I’ll look at putting it on NuGet but will have to wait and see.

Any questions or issues or ideas or comments then please tweet me @WestDiscGolf

Enjoy!

Friday, 25 March 2011

Thoughts and Musings about Microsoft WebMatrix

Webmatrix has been out for a while now and my initial thoughts when it arrived was “why?” and “what does this give you that can’t be done in the free versions of Visual Studio?”. So the only way to answer these questions was to download it and give it a whirl. After confirmation from ScottGu that it can easily co-exist next to Visual Studio (can’t live without it) I took the step and clicked download.

I’m not going to cover the installation process etc. here as its been covered a number of places and well its an installation process how interested in it are you really? ;-)

Initial thoughts

On firing up the IDE I was pleasantly surprised by the clean UI. It’s gone down the ribbon route (wouldn’t expect anything less from MS) and fits in well with the new mspaint (in Win 7) and Windows Live Writer to list a couple of applications.

The first main point that I found was I like the option of testing the sites in a multitude of browsers you have installed on your development machine. From a simple click of the button it will fire up in the specified browser. After issues with Firefox 3 and offline mode when on the commute to work and not working I’ve just tried this in Firefox 4 after upgrading last night and this seems to have been resolved. However, it’d still be nice to be given the option of doing the development in a new instance of the browser so it doesn’t compromise your open tabs if anything went wrong as when you’re on the train to work you can’t get them back until you get to the office.

Downsides

Being a long time Visual Studio user you will notice a number of problems you will need to get use to quite quickly when using Webmatrix … the biggest issue I was frustrated with was the lack of tooling support for intellisense when writing code in Razor (and other code) files. You could argue that intellisense is a selling point for Visual Studio.

Ok, so I can live without intellisense, but other tooling which would have been nice would have been support for dragging javascript files from the file listing of the site onto the page and it automatically adds the correct script tag etc. with a path to the script. Is that too much to ask for? This with a combination of lack of intellisense makes it not straight forward to a novice user to add in references without having to go and search for an example on the interweb.

Another issue, from a Visual Studio user perspective, is that you can’t launch the current site in a browser by hitting f5.

Positives

Positively I like the support for multiple optional template sites, I like the options where you can download other templates and they’ll essentially just work “out of the box”. This is especially important for novices who don’t know (or care) about sorting out databases or project references etc. to make the thing work.

The other big thing I like is the integration with Visual Studio which can be done with a simple click of the button. This is a pretty nice way to get around the intellisense issue but also allow for other tooling support you only get from Visual Studio. This makes working on a site between the two IDEs work pretty well and something that the Expression studio people should look into to improve the UX/Dev integration (potentially?).

However, this nice feature leads me onto the final thoughts …

Why bother with Webmatrix?

As a professional software developer who spends ~90% of this time using Visual Studio and is use to the shortcuts and using such plugins as Resharper I can’t see me using Webmatrix in anger with anything really, unless for some crazy madness reason I need to do some php *shock* or I need to setup a cms quickly to then load it into Visual Studio to do some monkeying about with it. Just from a pure tooling perspective it wins hands down and its what I’m use to. It might be like using a sledge hammer to put up a picture frame, but if you’re adept at wielding a sledge hammer then it can be used for multiple purposes.

Let me know your thoughts in the comments below or you can tweet me on @WestDiscGolf.