May 24 2012

What is the best way to verify cookies are enabled for html 5 (mobile) website using asp.net mvc 3

I know that similar questions have been asked here but I am nevertheless having trouble getting it to work.

I am writing an html 5 website using ASP.NET MVC 3 that is targeted to mobile devices.

I had previously asked another question about how to store and retrieve expensive data and decided to go with Session variables.

I finished the app and started internal testing and all was well until we realized that the web site is not working on some iPhones. After researching I came to realize that Session variables depend on cookies and that some iPhones ship with cookies disabled by default.

Until I come up with a better solution, I want to check to see if cookies are enabled and redirect to an error page.

I was hoping that ALL of the code to do this could be written in a base controller but I could not figure out how to do it. So instead I put the following code into the base controller...

protected void WriteCookie()
{
    Response.Cookies.Add(new HttpCookie("cookie_test"));
}

protected bool CookieCheck()
{
    return (Request.Cookies["cookie_test"] != null);
}

Then in a home controller I am calling the WriteCookie method before loading the view. The view itself has javascript that is redirecting to one of two controllers / actions. The controller / view that will usually be called (because html 5 could not resolve the location) is the ChangeLocation / Index controller/action. In there I have the following code...

if (!CookieCheck())
{
    RedirectToAction("CookieRequired", "Error");
}

Even with cookies disabled it is not redirecting to the above error page.

So what I am looking for is a best practices approach for solving this problem with a fairly complete code sample.

My home controller is pretty much ALWAYS redirecting to another page (in the javascript not the controller). I have a total of about 4 controllers with a total of about 6 actions.

My ideal would be to solve this problem completely in the base controller. If not that then I would love for it to work no matter what valid URL is typed (in other words, even if the web app is not entered through Home / Index). If not that then at least I want what I have shown in my code to work (set the cookie in Home / Index and check for it in the other controller / actions) and work even if the redirect is happening in a controller.

May 17 2012

Comment by Seth Spearman on What is the use case for using a JsonResult action in asp.net mvc3?

Awesome and very complete answers. Thanks for the illustration. Totally filled in the blanks for me. Seth

May 16 2012

What is the use case for using a JsonResult action in asp.net mvc3?

When is it typical to use the JsonResult action in an ASP.NET MVC 3 application?

From where is the JsonResult usually called; from another action or an actionlink rendered in the html?

Can you give me some examples where you want json instead of a typical view?

May 16 2012

What is the use case for using a JsonResult action in asp.net mvc3?

When is it typical to use the JsonResult action in an ASP.NET MVC 3 application?

From where is the JsonResult usually called; from another action or an actionlink rendered in the html?

Can you give me some examples where you want json instead of a typical view?

May 06 2012

Comment by Seth Spearman on How do I include ul tags inside of a razor code block?

Thanks for the link. Just what I needed.

May 06 2012

Comment by Seth Spearman on How do I include ul tags inside of a razor code block?

Thanks for not only telling me how but also the explanation of what I was doing wrong.
May 06 2012

How do I include ul tags inside of a razor code block?

How come the following code works fine...

        <ul class="searchList">
            @if (Model.Count() > 0)
            {
                foreach (var partner in Model)
                {
                    <li>
                        @Html.ActionLink(@partner.Name, "Details", "Partner", new { id = partner.AID }, null)<br />
                        @partner.Street<br />
                        @partner.CityStateZip<br />
                        @if(!string.IsNullOrEmpty(partner.Phone))
                            {
                               @partner.Phone<br />
                            }
                        @(partner.Distance) miles<br />
                    </li>
                }
            }
        </ul>

But this code does not work fine...

            @if (Model.Count() > 0)
            {
                <ul class="searchList">

                        foreach (var partner in Model)
                        {
                            <li>
                                @Html.ActionLink(@partner.Name, "Details", "Partner", new { id = partner.AID }, null)<br />
                                @partner.Street<br />
                                @partner.CityStateZip<br />
                                @if(!string.IsNullOrEmpty(partner.Phone))
                                    {
                                       @partner.Phone<br />
                                    }
                                @(partner.Distance) miles<br />
                            </li>
                        }

                </ul>
             } 

The second error one returns the following error...

Compiler Error Message: CS0103: The name 'partner' does not exist in the current context.

I am finding the code mixing rules of Razor to be difficult to follow. Any link that gives the canonical explanation will be appreciated.

Seth

May 04 2012

Comment by Seth Spearman on In Mvc 3 razor view what is the best way to conditionally render html based on Nulls in the model

I edited my question. Can you take a look again. What if it is already in a razor code block?

May 04 2012

In Mvc 3 razor view what is the best way to conditionally render html based on Nulls in the model

This is I am sure an easy question but I am having trouble figuring this out.

I want to do something like this....

@(string.IsNullOrEmpty(Model.CustomerUrl) ? "" : <a href="@Model.CustomerUrl">Click me</a>)

This snippet doesn't work.

The mixing of the html with the razor syntax and the inclusion of quotes in the attributes of the tags is making it hard to figure out.

I love razor except figuring out this kind of stuff is really tripping me up.

I would love to just not render the following at all if the CustomerUrl was null or empty...

<p class="customerLink links"><a href="@Model.CustomerUrl">@Model.CustomerName</a></p>

EDIT
This is still not working for me...thanks for the suggestion though.

My issue is that the above code is ALREADY in a Razor Code Block. Here is my actual code that I cannot figure out...

EDIT NUMBER TWO - THE following code is now working

    @if (Model.Count() > 0)
    {
        foreach (var partner in Model)
        {
            <li>
                @Html.ActionLink(@partner.CustomerName, "Details", "Customer", new { id = Customer.AID }, null)<br />
                @partner.Street<br />
//this is what i cannot figure out!!
                @if(!string.IsNullOrEmpty(partner.Phone))
                    {
                        @partner.Phone@:<br />
                    }
                @partner.Distance<br />
            </li>
        }
    }

I preceded the nested block (the if) with the @ symbol. Then the markup
I had to delimit with @: Then it worked.

It seems yesterday when I tried to use a nested razor code block I got a compiler error BECAUSE I preceded it with an @. So now I am more confused than ever.

In fact...if I tried to surround my @partner.Phone with quotes like this... "@partner.Phone"@:</ br> I get another compiler error. Razor is great when it works but when it doesn't it is very confusing.

Seth

May 03 2012

Comment by Seth Spearman on How do I troubleshoot Visual Studio which is hanging when loading ASP.NET MVC project?

I fixed it before trying this but I think this is a good suggestion.