Blog Stats
  • Posts - 36
  • Articles - 2
  • Comments - 17
  • Trackbacks - 14

 

Monday, July 07, 2008

ScriptControl question

As of late I have gotten a chance to use the ASP.Net AJAX extenders and script controls, and so far am really liking how they work, though it would be nice if adding the .js was a little cleaner then manually adding them to the assembly wether in the AssemblyInfo.cs or the controls cs file.

I have to say I really like the extender and script controls that come with the ASP.NET AJAX. They really are so much nicer to work with then building up Javascript strings in the CS file of the server control.

I do have a question about them though. One of the ScriptControls I made uses a webservice to pull data if the service name is set. At the moment I have it dynamically registering an JSON script service based webservice. Unfortunately I have found that this is not an optimal solution since the name of the Javascript object created by the ScriptService changes depending on the namespace and class name of the service.

I am sure that there is a better way to do this, but I am not exactly certain of it. Any suggestions or links would be appreciated.

Wednesday, May 21, 2008

Lazy list

I have been following Rob Conery's posts on the MVC Storefront, and trying the repository/pipes-filters for data access that he has been using.

While trying out the implementation of a LazyList he was using, I had noticed that the example table was being joined onto the category table. At the time however, I really had not thought much of it, until I read MVC Storefront: Brainbender Intermission which got me thinking.

Admittedly, I don't really have to much of a problem with it loading all of the examples for all the categories at one time at the moment. However, I figured it would be nice to have it work the way intended, and query the database for the examples in a category only when asked for.

I liked the idea of the LazyList, and since I was already using a Service class for all data access, along with setting up the category hierarchy I moved the creation of the LazyList of examples property into the the GetCategories method of the service class.

It seems that as long as the LazyList is created after the categories .ToList() call it properly works. So I ended up with this code:

            categories.ForEach(category =>
            {
                category.Examples =
                    new LazyList
                        <DevExamples.Data.Example>(
                        from e in repository.GetExamples()
                        where e.CategoryId == category.ID
                        select e);

                var subCategories = (from sub in categories
                                     where sub.ParentId == category.ID
                                     select sub).ToList();
                category.SubCategories = subCategories;

                subCategories.ForEach(c => c.Parent = category);

            });

 

Technorati Tags:

Thursday, September 20, 2007

Custom Rss Admin feeds

I fixed up the Rss Admin feeds so that modifying web.config (along with new handler classes) would not needed to add a new Admin Rss feed.

Also added a simple form to allow administrators to created their own. It is a bit limited, but adding additional options should not be to hard. Though those will have to wait till a later date.

On a side note, I found that if I had the option of returning the non-generic  base class, or a generic subclass it makes the code a lot more flexible if you return the non-generic base class if you do not have a need to have the generic type.

 

 

Technorati Tags:

Sunday, April 20, 2008

The joys of spam

I woke up this morning to 600+ bounce messages in my inbox. And over the course of the day probably another 600+.

Apparently someone decided that it would be fun to send a spam message around 11:40 last night using my email address.

Hopefully this was a one time thing. So I don't have to try to figure out where all I need to change this email address at.

Tuesday, March 18, 2008

.Net based SVN client library

As an exercise to figure out socket programming I have been messing around with creating an SVN client library to use with DevExamples.com, well actually a generic SC client library that would allow me to change what repository site I was working against, SVN just seemed like the simplest of them at the moment.

However, after thinking about it, I think it would be better to use one that is already built if possible. I just have not been able to find a pure .Net client library. It is also seems rare for a source control library to work without a working folder, which is somewhat limiting when I want to check the propagation of examples using this method on the same machine.

Though I think it would be interesting to write a LINQ to Source provider, as a way to learn expression trees better. Plus it would be nice to be able to not only easily switch hosting providers (the reason I am looking for a pure .Net library) but also to have a standard syntax to allow me to easily switch from SVN to SourceGear's Vault (which I use for my personal development projects) to TFS (which we use at work) as the storage system for the examples.

Though SvnBridge could possibly help with that too.

Technorati Tags: ,,,

Tuesday, December 18, 2007

I feel so behind sometimes

I was trying to determine the best way to secure my admin* actions when I thought of something I had only really seen while studying for my MCSD.Net exams, or maybe it was the MCPD upgrade exams I haven't gotten round to taking.

I decided to secure the admin controller actions using.

[PrincipalPermission(SecurityAction.Demand,Role="<Role>")] 

Which I had never used even though I started programming .Net in during the beta of 1.0. And I have to admit that each time I run into something like this, it makes me feel that my thought in a previous post is right.

This works for my immediate needs, after thinking about it, it would be nice if the Route Validation could deal with security too. That way instead of getting "Security Exception" when navigating to one of the admin actions, the route would simply not resolve resulting in "An action named 'New could not be found on the controller." if someone was trying out the obvious Urls for maintenance, since they are rather hackable (from Phil Haack's recent interview on the ASP.Net Podcast show ). 

Though now that I write that I think it would be better to have it throw the Security Exception, log it, then handle both errors be handle it as a 404 error from then on. That way I can log information of the unauthorized attempts on the admin actions, without telling the outside world anything. So maybe I don't need to be able to specify authentication validation on the routes after all. Though routes based on errors might be nice.

Now I just need to look up more on custom error pages.

I really do feel so behind sometimes.

* Will be making a post in next few days on this. The dynamic stuff I had started for the last post was a bit more usable in the admin area.

Technorati Tags: ,

Temp service provider implementation

Well, Bill Pierce in his post about making the HttpContext wrapper use the application as a service provider got me to a good enough solution for the script service I had wanted in my previous post.

While not quite as clean as adding it to the HttpContext, I think an extension method on HttpApplication like this will work well for now.

public static object GetService(this HttpApplication application,Type service)
{
    IServiceProvider provider = application as IServiceProvider;
    if (provider == null)
        return null;

    return provider.GetService(service);
}

Then add/implement the IServiceProvider on the Global class. Allowing me to get an instance of the script service using 

IScriptService scriptSvc 
    = this.HttpContext.ApplicationInstance.GetService(typeof(IScriptService)) as IScriptService;

Though though the more I think of it, the more likely I am to stick this method on the Ajax and Html helper classes. I wonder how hard it would be to add an interface like IMvcHelper to both the AjaxHelper and HtmlHelper Classes my extension method would be available to both, without duplicating code.

Friday, December 14, 2007

Login via Usercontrol View + jQuery

Since administration was the next part I wanted to work on.

I decided that for the moment I just want a little login for in the upper corner like

image

I decided to put the login screen view logic into a user control, and added the following user control named Login.ascx and placed it into the Shared

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>

<%if(!Page.User.Identity.IsAuthenticated) {%>
    <form action="/Home/Login" method="post" id="LoginForm">
        <span id="EmailLabel">Username:</span><br />
        <%=Html.TextBox("Username")%><br />
        <span id="PasswordLabel">Password:</span><br />
        <%=Html.Password("Password")%><br />
         <span id="Error">
            <%=ViewData.ContainsDataItem("Message") ? ViewData["Message"] + 
"<BR />" : ""%> </span> <%=Html.SubmitButton("Submit", "Login")%> </form> <%}else {%> <span>Not <%=Page.User.Identity.Name %><br /> <%=Html.ActionLink<DevExamples.Controllers.HomeController>(c => c.Logout() , "Logout", new { id = "LogoutLink" })%></span> <%} %>

And added the user control to the masterpage using

<div id="Login"><%=Html.RenderUserControl("~/Views/Shared/Login.ascx") %><div>

I was going to use

using (Html.Form<DevExamples.Controllers.HomeController>(c=>c.Login()
,System.Web.Mvc.FormExtensions.FormMethod.post,new {id="LoginForm"}))

to create the form (because it is a lot cooler), but couldn't get the html attributes to work and was more interested in getting the dynamic stuff working then looking at the code for the extension method in the MVCToolKit, in other words being lazy.

I thought about putting the authentication checking logic into the controller. But handling it this way made it a bit simpler, and more self contained, so until I find a compelling enough reason I will be leaving it this way.

After I had the user control showing up, I decided to hook up the actual authentication. I decided to go with forms authentication, and set it up using the image button in the solution explorer.

Next it was time to create the login and logout controller actions, which I decided would best fit in the HomeController, At first I had thought to create two separate views, one for when logged in and one for logged out, then remembered Phil Haack, at least I think it was him, something about being able to use user controls in the RenderView method of the controller.

I ended up with the following two controller actions:

[ControllerAction]
public void Login()
{
    if (!ControllerContext.HttpContext.User.Identity.IsAuthenticated)
    {
        if (Request.RequestType == "POST")
        {
            string userName = Request.Form["Username"];
            string password = Request.Form["Password"];

            if (Membership.ValidateUser(userName, password))
            {
                FormsAuthentication.SetAuthCookie(userName, true);
                //Set cookie and redirect
                RedirectToAction("Login");
            }
            else
            {
                ViewData.Add("Message", "Invalid Username/Password");
            }
        }
    }
    RenderView("Login");
}
[ControllerAction]
public void Logout()
{
    FormsAuthentication.SignOut();
    RedirectToAction("Login");
}

Since it was allowing me to just render out the user control, I decided there was no point to refreshing the whole page, so decided to use a AJAXish way of doing it. So I decided it was time to pick a javascript library. And since Phil had provided a link to  Using script.aculo.us with ASP.NET MVC by Chad Myers,I decided to go with jQuery for this (that and it seemed to fit better).

After a bit of reading through their tutorials I added this to the header section of the master page.

<script src="../../Content/jquery.js" type="text/javascript"></script>
<script src="../../Content/jquery.form.js" type="text/javascript"></script>
<script type="text/javascript">
    var readyFuncs = function() { 
        $('#LoginForm').ajaxForm(function(result) { 
            $('#Login').html(result); 
            $(document).ready(readyFuncs); 
        }); 
        $('#LogoutLink').click(function(){
            $.get('/Home/Logout',function(result){
                $('#Login').html(result);
                $(document).ready(readyFuncs); 
            });
            return false;
        });
    };
    $(document).ready(readyFuncs);
</script>

The only real snag I had run into with this was that each time the Login div was refreshed I had to reregister the events.

Friday, January 04, 2008

Being blocked by internal/private

I ran into a situation that reminded me of Steam Harman's post about A New Year's Resolution for Developers in which he states that by default methods should be public and virtual unless there is a good reason not to.

This started when after I decided I was missing functionality like RegisterClientScriptBlock, allowing me to add a client script only if one of the controls needs it, along with only adding it once. I was also missing the ability to use designers.

To this end, I decided I was going to make a simple server control library with server controls that would provide the functionality found in the MVCToolkit Html extension methods. I also figured it would be nice if I could allow them to function in WebForms as well, if they are on a runat server form tag.

After a while I found the VerifyRenderingInServerForm method on the Page, which seemed to be the only method available for this. Unfortunately while I can override this method after loading up reflector to see how the base Page was determining that the control is in a form tag, I found that I could only do it with a try/catch around base.VerifyRenderingInServerForm.

While I don't really have much of a problem doing this, there is a little bit of nagging in the back of my head over the fact that the Page class contains a method called IsInOnFormRender, which has exactly the information I want. Unfortunately this property is internal, preventing me from retrieving efficiently retrieving this information.

The other problem I find with this is that these server controls would need to run on a special page in order to have the desired functionality, have each of the controls that while have the functionality do the try/catch on VerifyRenderingInServerForm, or use reflection to get access to that variable. None of which are particularly optimal.

On a side note, I wish I had found this method when I years ago when I was writing the controls of the framework we use at work. Since I could not have our controls running within a runat server form tag because we submit the data through an IFrame, overriding VerifyRenderingInServerForm would have allowed me to stop them from throwing the error.

Technorati Tags: ,

Monday, December 24, 2007

Got my nice Urls working

Its amazing how fast things can be done when not fighting bad assumptions. In this case it was that my admin URLs needed to use the nice descriptive URLs, instead of just /[Controller]/[Action]/[id] type routing.

Once I realized I was making things overly complicated I, I ended up with a set of routing functions that are actually a bit less complicated then what I would have come up with if I had been able to extend the RouteTable the way I had planned originally.

I also decided that I didn't like the idea of having the make every category and example name unique. Most of this was that I'm not the creative, but I also realized that it would be really ugly I wanted to have namespaces as categories. For example, it would not have been possible to have both C/System/Windows/Forms/Control and C/System/Web/UI/Control since Control would have had to be unique.

I also decided that I was going to set a limit to the depth of categories that the site was going to have, though it is easy to update this. First I made a function that would generate routes similar to those previously posted.

protected void CreateRouteSet(string baseRoute, string baseName
    , int maxDepth, object Defaults)
{
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < maxDepth; i++)
    {
        if (sb.Length > 0)
            sb.Append("/");

        sb.AppendFormat("[{0}{1}]", baseName, i);

        RouteTable.Routes.Add(new Route{
         
            Url = String.Format(baseRoute,sb.ToString()),
            Defaults = Defaults,
            RouteHandler = typeof(MvcRouteHandler)
        });
    }
}

protected void Application_Start(object sender, EventArgs e)
{

    CreateRouteSet("C/{0}", "Category", 6,
        new { Action = "Index", Controller = "Categories" }
        );

    //Other routes using the standard [Controller]/[Action]/[Id]

}

This will setup routes to handle category hierarchies up to 6 deep.  And add Category1,Category2... into the RouteData.Values collection in the controller.

Next I set up the code to allow me to handle the route, which I did in the model, adding a method that takes an IDictionary<string,object>.

public Category GetCategoryFromRouteData(IDictionary<String,object> data)
{
    //Grab just the category data
var navList = from route in data where route.Key.IndexOf("Category") == 0 orderby route.Key select new{Key = route.Key, Value = route.Value.ToString()}; string sqlSelect = @" SELECT {0}.* FROM Category {0} {1} WHERE {0}.Name = {{0}}";
//{{{2}}} will be replaced with {{<param index>}}
string join = @"INNER JOIN Category {0} ON {0}.[Key] = {1}.ParentKey AND {0}.[Name] = {{{2}}} //This is the name of the category I am actually interested in
var mainCat = navList.Last(); //Build a list of category values to for parameterized query
List<string> values = new List<string>(); values.Add(mainCat.Value); StringBuilder sb = new StringBuilder(); string lastCategory = mainCat.Key; int i = 1; foreach (var item in navList.Reverse().Skip(1)) { sb.AppendFormat(join, item.Key, lastCategory,i++); values.Add(item.Value); lastCategory = item.Key; } string sql = String.Format(sqlSelect,mainCat.Key, sb.ToString()); Category cat = this.ExecuteQuery<Category>(sql, values.ToArray()).Single();


//GetDisplayCategory skips over categories that have only 1 subcategory
//and no examples, allowing me to have the planned Url structure without
//
requiring the user to navigate through several empty categories.
return GetDisplayCategory(cat); }

I had planned on having doing it all LINQ to SQL expressions, but it was taking more time then it was worth to figure out how to do the hierarchy lookup from the category names.  I need to add some caching of the key lookups at some point, since the joins look like they might get a bit nasty for deep categories, but that can wait.

I added a few overloads to the HtmlHelper ActionLink extension methods to handle making the nice Url links.

Now I need to figure out how to deploy the usercontrols that will contain the executable part of the example. I have had some problems with compilation sometimes when I had the website directly upload the usercontrols. Plus I would like them to go into source control when I add them.

Technorati Tags:

 

 

Copyright © Sean Lynch