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"/>
4

2 回答 2

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...

于 2011-09-06T15:38:02.213 回答
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);
于 2011-09-06T15:56:21.173 回答