0

I have been asked to migrate some code to our new asp.net web app. but I am extremely unfamiliar with asp.net.

The following block of code detects the user device and changes the url accordingly. I need to make the same logic using asp.net / c# but I have no clue where to start.

Any help at all is very appreciated.

    <xsl:variable name="useragent" select="lower-case(request:getHeader($request, 'user-agent'))"/>
<xsl:variable name="is_iphone" select="string(contains($useragent, 'iphone;') or contains($useragent, 'ipad;') or contains($useragent, 'ipod;'))"/>
<xsl:variable name="is_blackberry" select="string(contains($useragent, 'blackBerry'))"/>
<xsl:variable name="is_android" select="string(contains($useragent, 'android'))"/>
<xsl:variable name="application_url">
    <xsl:choose>
        <xsl:when test="$is_iphone = 'true'">
            <xsl:value-of select="f:getEnvParameter(concat('url.app.iphone.', $param_client), '')"/>
        </xsl:when>
        <xsl:when test="$is_blackberry = 'true'">
            <xsl:value-of select="f:getEnvParameter(concat('url.app.blackberry.', $param_client), '')"/>
        </xsl:when>
        <xsl:when test="$is_android = 'true'">
            <xsl:value-of select="f:getEnvParameter(concat('url.app.android.', $param_client), '')"/>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="''"/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:variable>
<xsl:variable name="application_url_exists" select="string-length(string($application_url)) != 0"/>
JFFF
  • 991
  • 4
  • 17
  • 31
  • How much of this XSL code do you have to convert? You know .NET can run XSL as-is... you'd just have to provide extension methods to mimic the environment provided by whatever library `f` is mapped to (which you'd probably have to do anyway). – harpo Sep 06 '11 at 15:41

2 Answers2

2

Something like this:

//create your application url - for whatever you use this for after
            var applicationUrl = string.Empty;
            //Get the user agent
            var userAgent = Request.ServerVariables["HTTP_USER_AGENT"];
            //test the useragent and set application url
            if(userAgent.Contains("blackBerry"))
            {
                applicationUrl = "url.app.blackberry";
            } else if(userAgent.Contains("android"))
            {
                applicationUrl = "url.app.android";
            }...etc

Then use your applicationUrl however you need to later...presumably some redirect or other...

Timbo
  • 4,505
  • 2
  • 26
  • 29
1

Something like this should get you most of the way there. If you can provide details on how to concatenate the URL, I will update my answer:

Depending on if "app" changes at all, this might be the simplest way:

string applicationUrl = String.Format("mysiteurl.app.{0}", Request.UserAgent.ToLower());

If you need to do further manipulation to the path based on the user agent, you can do something like this:

string userAgentPath = String.Empty;

switch (Request.UserAgent.ToLower())
{
    case "iphone":
        userAgentPath = "app.iphone";
        break;
    case "blackberry":
        userAgentPath = "app.blackberry";
        break;
    case "android":
        userAgentPath = "app.android";
        break;
}

string applicationUrl = String.Format("mysiteurl.{0}", userAgentPath);
James Johnson
  • 45,496
  • 8
  • 73
  • 110
  • Thank you very much for your help. I think I will be able to manage the rest of the coding from here using both answers as examples. – JFFF Sep 06 '11 at 15:59
  • @JFFF: Edited my answer. Depending on what you need to do, the first suggestion is probably the easiest. – James Johnson Sep 06 '11 at 16:10