Record of my first class VB.NET

I am trying to make 2 variables available on my site. I am parsing the url to recover both of them.

This code on the page itself works great.

Dim countryLanguage As String
countryLanguage = (Request.ServerVariables("URL"))
Dim langVar = (Mid(countryLanguage, 2, 2))
Dim countryVar = (Mid(countryLanguage, 5, 2))

I created a class file whose code is below. In doing so, I get a compilation error (BC30451: Name 'Request' not declared.).

Public Class url_parser

    Public Shared Function urlVars(ByVal langVar, ByVal countryVar) As String
        Dim countryLanguage As String
        countryLanguage = (Request.ServerVariables("URL"))
        Dim langVar = (Mid(countryLanguage, 2, 2))
        Dim countryVar = (Mid(countryLanguage, 5, 2))
    End Function

End Class

thank

+3
source share
3 answers

You can use System.Web.HttpContext.Current.Requestto get the request object for the current thread.

The best way to get folders for your country and language is to use Request.Url.Segments.

Public Class url_parser
    Public Shared Function urlLanguage() As String
        Dim Request = Web.HttpContext.Current.Request
        Return Request.Url.Segments(1).TrimEnd("/"c)
    End Function

    Public Shared Function urlCountry() As String
        Dim Request = Web.HttpContext.Current.Request
        Return Request.Url.Segments(2).TrimEnd("/"c)
    End Function
End Class

Access this static function this way.

Dim MyLanguage = url_parser.urlLanguage
Dim MyCountry = url_parser.urlCountry

In this example, if Url is "/ en / us /", then ...

  • Segment (0) - "/"
  • Segment (1) - "en /"
  • (2) - "us/"
+1
System.Web.HttpContext.Current.Request

System.Web HttpContext.Current . , .

, (, HttpContext) . , . , MVC ( ). , , , : *

+4

Request .

countryLanguage .

If you really need access to the Reqeust object from a class (not recoemmended), use:

HttpContext.Current.Request
0
source

Source: https://habr.com/ru/post/1756637/


All Articles