Using Razor, CSHTML, ASP.NET Web Pages, WebMatrix as a web service - More Wally - Wallace B. McClure
in

MoreWally.com

Giving people what they want, More Wally. This is the technical and personal blog site of
Wallace B. (Wally) McClure.

This Blog

Syndication

Technical Sites

Archives

More Wally - Wallace B. McClure

This blog will have all kinds of posts about Wally McClure. In it, there will be tons of .NET and computer programming posts as well as Wally's views on life in general. As you might guess, this site and blog help you get More Wally in your life. What more could anyone want? iPhone, Android, Xamarin.iOS, Xamarin.Android, Mobile, HTML5, .NET, ADO.NET, ASP.NET, AJAX, jQuery, jQuery Mobile, ASP.NET AJAX, and Windows Azure............follow me on twitter at Wally

Using Razor, CSHTML, ASP.NET Web Pages, WebMatrix as a web service

I've been trying to learn about Razor, CSHTML, ASP.NET Web Pages, and WebMatrix.  I've been writing some examples specifically targetting HTML5 mobile apps.  After all, the best way to learn a technology is to dive into it.  I wanted to call some web services in a web page, so of course, I was going to use CSHTML.  I wrote some code that didn't need any parameters, that was pretty easy.  However, what do you do when you want to pass some parameters in.  There are two solutions I can think of:

  • Pass the params in the QueryString, perform a GET operation, and request the items in the CSHTML file.  This is easy, simple, and makes sense.  You code would need to protect the entries that are passed in on the url, but hey, you already know how easy it is to change them.  So, this is what I am doing here.
  • Pass the params in the Body, perform a POST operation, and request the items in the CSHTML file.  This takes a little more work, but I think its a better way to do things. 

I decided to pass in a GET because I just needed to do something simple and get data.  I like REST principles, but I'm no zealot that things have to be done X way.  Here's my javascript client side code using jQuery and jQuery Mobile:

function WhereIsUserNow() {
var UserToView = "@UserToView";
$.ajax({
type: "GET",
url: "@Href("~")Services/MostRecent.cshtml?UserToView=" + UserToView,
dataType: "json",
contentType: "application/json",
success: function (outPut) {
WhereUserHasBeenMostRecent(outPut);
},
error: function (xml, err) {
alert("err:" + xml.responseText);
}
});
}
else {
alert("Please select a user before trying this tab.");
}
}

And now here's my server side cshtml code:

@{   
   
string UserName = Membership.GetUser().UserName; 
    string UserToView = Request.Params["UserToView"];
    //do a bunch of things.
    Response.Write(Json.Encode(UserNow));
}

Finally, my code will send json data out the client web browser.  Thankfully, it all seems to be working.  Probably need some added security somewhere, but who doesn't. 

My second point in this is that I'm not saying that Razor/CSHTML is the best place for writing a web service.  I'm sure that there are security holes in this approach that have nothing to do with the technology.  Anyway, here it is, enjoy.

PS. I had to make a hand edit on this. Hopefully, I didn't muck it up too much.

Comments

 

Twitter Trackbacks for Using Razor, CSHTML, ASP.NET Web Pages, WebMatrix as a web service - More Wally - Wallace B. McClure [morewally.com] on Topsy.com said:

Pingback from  Twitter Trackbacks for                 Using Razor, CSHTML, ASP.NET Web Pages, WebMatrix as a web service - More Wally - Wallace B. McClure         [morewally.com]        on Topsy.com

December 17, 2010 12:51 PM
 

Dew Drop – December 17, 2010 | Alvin Ashcraft's Morning Dew said:

Pingback from  Dew Drop – December 17, 2010 | Alvin Ashcraft's Morning Dew

December 17, 2010 7:59 PM
2006 - Wallace B. McClure
Powered by Community Server (Non-Commercial Edition), by Telligent Systems