简体   繁体   中英

Redirect to another html page

Is there a way to redirect my application to a webpage after checking the browser version first?

I'm using C# to run my angular app and the index.html is loaded by default, but is there a way to control that ?

Eg : if my browser is IE load wrongBrowser.html otherwise load index.html (the default one)

Note that i dont want to redirect my page because i want to keep the orignal url : ex localhost/api/search=text. If i do a redirect, it will overide my url. So i just want to load the html content

Im using C# with visual Studio for the server side

The first page of your app will have to load as it needs to be able to determine the browser specs. Only then can you then redirect to another page based on that knowledge.

I have never used Angular JS neither Angular with C# , but from personal knowledge I know you can "redirect" without changing the url, using a XML request in vanilla javascript (maybe you can place this somewhere):

var request = new XMLHttpRequest();

request.addEventListener("load", function(evt){
    document.write(evt.target.response);
}, false);

request.open('GET', 'a.html', true),
request.send();

Now what this does is simple, we set variable request which is a XMLHttpRequest object, then we set an event listener for when it loads, after it loads we replace the code in the page with the targets code, we then set the url and send the request.

I have only used this for testing, so there might be issues that I don't know of, but it does import the html code in.

In C# you can do the following using Request.Browser :

if(Request.Browser.Browser.IndexOf("InternetExplorer")){
   return View("wrongBrowser");
}

You have to use IndexOf due to the fact that most, if not all browsers return their version in their name too.

Here's a list of possible browser strings:

  • IE <= 11: InternetExplorer <numeric version>
  • Edge: Edge <version>
  • Safari: Safari <version>
  • Chrome: Chrome <version>
  • Opera: Chrome <version>

There are more, most of them will come under Chrome though. I will not go into much detail as you specified IE11 which is listed above. The above method is not really reliable for other, less popular browsers so keep that in mind.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM