to rewrite url in Global.asax, ASP.NET - asp.net

HEllo. I need replace double slashes in one slash. I am planning do this in Global.asax Application_BeginRequest event. Is it enough? or better do a http module?
Thank you.
UPD
Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
' Fires at the beginning of each request
Thread.CurrentThread.CurrentCulture = New Globalization.CultureInfo(AppSettings.UsedCulture)
Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture
Dim retUrl As String
....
some code
....
'Dim app As HttpApplication = CType(sender, HttpApplication)
'app.Context.RewritePath(retUrl)
Dim myContext As HttpContext = HttpContext.Current
'Rewrite the internal path
myContext.RewritePath(retUrl)
End If
I am using .Net 1.1. And It must be on .Net 1.1
RewritePath does not rewrite URL.Why?
UPD2
Having decided to make addition redirect in Sub Application_BeginRequest with new rewritting url.

If you're using IIS 7.0 (and newer), you can use the IIS UrlRewrite module. We've been using it for a while, and have no complaints.

Related

RestSharp Asp.net VB.net no response

I am trying to pass api request and get a response on button click in Asp.net 4.61 with VB.net. Then the response will show in a textbox, however I never get a response and it doesnt even seem my login is hitting the API as no attempts show up in the API log. I had something like this working the other day and my stupid hard drive died! Trying to rewrite it but I have missed something clearly. I do not get any errors just no response.
I am able to use postman and get a response to the same url
thanks i advance here is a sample of my code
Imports Newtonsoft.Json
Imports RestSharp
Imports System
'Imports System.Text.Json
'Imports System.Text.Json.Nodes
Imports System.Threading.Tasks
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim restClient As RestClient = New RestClient("http://myurl.com/ws/rest/userid=myid&password=password")
' restClient.Timeout = -1
Dim request As RestRequest = New RestRequest(Method.Post)
request.AddHeader("X-API-Key", "MYAPIKEY")
request.AddHeader("Content-Type", "application/xml")
Dim body = "MY Body Request XML Redacted"
request.AddParameter("application/xml", body, ParameterType.RequestBody)
Dim result As RestResponse = restClient.Execute(request)
MsgBox((result.Content))
txtXMLTEXTBoxTest.Text = result.Content
End Sub

Add HttpHandler from HttpModule for static file not present in project

How to set httphandler from httpmodule for static file that is not present in poject.
Eg: If client requests a file called abc.xml thats not in my project,I need to add handler from my httpmodule and send some response to client how do i do it. My handler does not get called after adding it from httpmodule.
My HttpModule Code is as given below:
Public Sub Context_BeginRequest(sender As Object, e As EventArgs)
Dim Application As HttpApplication = CType(sender, HttpApplication)
Dim CustomHandler As StorageHandler = New StorageHandler()
Application.Context.Handler = CustomHandler
End Sub
The processrequest in customHandler does not get executed.
After spending good 5hr, Got it .
Need to use RemapHandler,
Application.Context.RemapHandler(CustomHandler)
and not
Application.Context.Handler = CustomHandler

Redirecting to www

I've added a global.asax to my web project, and I'm trying to check if the domain in the Begin_Request contains "www", and if it doesn't, then redirect them to the page they were requesting, but with the "www" pre-pended to it. For an example, If I try to access : SampleSite.com, it directs me to SampleSite.com/www.SampleSite.com/www.SampleSite.com/www.SampleSite.com/www.SampleSite.com/www.SampleSite.com/www.SampleSite.com/www.SampleSite.com/index.aspx
Here is the code:
<script runat="server">
Sub Begin_Request(ByVal sender As Object, ByVal e As EventArgs) Handles Me.BeginRequest
Dim domain As String = Request.Url.Host
If (Not domain.StartsWith("www")) Then
Dim path As String = ""
path = String.Format("www.{0}", Request.Url.AbsoluteUri.Substring(7))
Response.Redirect(path)
End If
End Sub
</script>
Any ideas?
I don't think you should do this by code but using IIS redirect features. Here you have an example about how to do it on IIS 7
In case you still want to stay with this approach, change
String.Format("www.{0}", Request.Url.AbsoluteUri.Substring(7));
by
String.Format("{0}://www.{1}", Request.Url.Scheme, Request.Url.AbsoluteUri.Substring(7));

Make asp.net webpage respond to WebClient request

I have run into a problem in my asp.net and vb.net programming. I have made a website which connects to a database to authenticate users. I have made another page which my windows form application uses to authenticate users so the program does not have to connect directly to the MySql Database as this is insecure.
So far, the code for my asp.net page is:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
If Request.QueryString.Keys(0) = "key" And Request.QueryString.Keys(1) = "getapikeyv1" And Request.QueryString.Keys(2) = "authapienc2" Then
Dim key As String = Request.QueryString.Get("key")
If key = "dua9r3rqpK{WPDWp9r93af03r8yqrEPIF{A}W~DW)D08q3raewdawdug9q8wdyaw9wq" Then
returnLabel.Text = "XYeJay4XVh6amvf1kJ34Y6y6hR0sSokP9oJFsi0Mxl4QB0T86W0iac9o2nRfCmFmsONq6sPz+rdX+/NqFk3inHRGnwnqf0IsQXE/M/SvnvY="
Else
returnLabel.Text = "invk"
End If
Else
returnLabel.Text = "invalidrequest"
End If
End If
End Sub
The default value for 'returnLabel' is INVALIDREQUEST. My api keys defined above are working fine so are not needed to be considered here. However, when I use the following code to communicate with the webpage, it always returns with returnLabel's value as 'INVALIDREQUEST'. Even if I put the returnLabel.Text = "1233" at the beginning of the Me.Load sub, the page still returns INVALIDREQUEST when I communicate with it through my program!
I use this code to request:
Try
Dim APIRequest As New WebClient
APIRequest.QueryString.Add("key", r9j0wqfha0fh0wf0.DecryptString128Bit(APIKey5, dVadaihwdq93ra0w0))
APIRequest.QueryString.Add("getapikeyv1", "")
APIRequest.QueryString.Add("authapienc2", "")
Dim ako39rqwfa As String = APIRequest.DownloadString(EncryptionPageUrl)
MsgBox(ako39rqwfa)
Dim m As Match = Regex.Match(ako39rqwfa, "<span id=""returnLabel"">.*?</span>")
APIKey00 = m.Value.Replace("<span id=""returnLabel"">", "").Replace("</span>", "")
Catch ex As Exception
MsgBox(ex.ToString)
End Try
As you can see, the WebClient I use is fine but the Me.Load sub in the webpage is not responding the request made by the program...
Is there any way I can make the webpage respond to the WebClient.DownloadString() function?
Thanks,
Rodit
(ps. c# or vb code would be appreciated)
Change your asp.net page to check the query string like this, not sure if you can assume they are in a certain order:
If Request.QueryString.Get("key") isnot nothing And
Request.QueryString.Get("getapikeyv1") isnot nothing And
Request.QueryString.Get("authapienc2") isnot nothing Then

Using and accessing Webresource

I know this has been asked before. Yes, i did my research but it doesn't seem to be working for me so i hope you experts can help me out :)
Here's what my Project looks like
http://i.stack.imgur.com/nnPZJ.png
Yes, the Build Action is Embedded Resource. I also added this in the AssemblyInfo
Assembly: WebResource("WFL.WebResource.EXT.XXX.png", "image/png")
So now, in the default.aspx i say
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim cs As ClientScriptManager = Page.ClientScript
Dim strReturn As String = cs.GetWebResourceUrl(Me.GetType(), "WFL.WebResource.EXT.XXX.png")
Dim strReturn2 As String = cs.GetWebResourceUrl(GetType(_Default), "WFL.WebResource.EXT.XXX.png")
Image1.ImageUrl = "http://localhost" + strReturn
Image2.ImageUrl = "http://localhost" + strReturn2
Response.Write("http://localhost" + strReturn)
Response.Write("http://localhost" + strReturn2)
End Sub
But when accessing the returned URL, i get The resource cannot be found.
What am i doing wrong?
Thanks in advance.
This being VB and not C#, you don't need the .EXT portion in WFL.WebResource.Ext.XXX.png. That is something C# needs (to specify folder paths), but VB doesn't. You just want your namespace then resource. Try WFL.WebResource.XXX.png and see if that works.

Resources