How do I validate for a empty query string parameter in asp.net mvc3

I want to validate for a empty Id value in the url.

../Task/EditEmployee/afccb22a-7cfd-4be5-8f82-9bd353c13b16

I want that if the Id is empty

../Task/EditEmployee/

Than redirect the user to a certain page.

public ActionResult EditEmployee(Guid Id)

{

//Some code in here

}

Answers


It may not be the best solution but you can take id parameter as string and try to parse it like this:

public ActionResult EditEmployee(string id)
{
    if(string.IsNullOrWhiteSpace(id))
    {
        // handle empty querystring
    }
    else
    {
        Guid guid;
        if (Guid.TryParse(id, out guid))
        {
            //Some code in here
        }
    }
}

Or

You can also create a regex constraint on the route but that may be too complicated and hard to understand. Map this route before the default one.

routes.MapRoute(
    "TastEditEmployee",
    "Task/EditEmployee/{id}",
    new { controller = "Task", action = "EditEmployee" },
    new { id = @"^(\{{0,1}([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){12}\}{0,1})$" } 
);

Then you can use id parameter as Nullable Guid.

public ActionResult EditEmployee(Guid? id)
{
    //do something
}

Since Guid is a struct, the value of Id will be Guid.Empty if it was omitted. You can check for that.

public ActionResult EditEmployee(Guid Id)
{
   if (Id == Guid.Empty) throw new ArgumentException("Id not specified.");
}

Need Your Help

Invoking .Net COM assembly from Powerbuilder application (without registration)

com interop manifest registration powerbuilder

We have a Powerbuilder 10 application that is using .Net COM assemblies. We are trying to embed the manifest in the PB application (to invoke COM assemblies without registration). The merged manifest

Git commit not working with Emacs and Ubuntu

git ubuntu emacs

I'm running it in vagrant on ubuntu 14.04. It was working fine before but now it's not.