[英]How to rewrite URLs without # on IIS with AngularJS?
I've used an URL rewrite in my web.config that will allow me to add URLs that do not include a # and allow me to view URL's such as http://somewebsite.com/name using "name" as a parameter to retrieve a template file. 我在web.config中使用了URL重写,这将允许我添加不包含#的URL,并允许我使用“名称”作为参数来查看URL,例如http://somewebsite.com/name。检索模板文件。 When I use this param with an XMLHTTPRequest the pages always return a 200 response because all URLs are essentially validated by the web.config rewrite. 当我将此参数与XMLHTTPRequest一起使用时,页面始终返回200响应,因为所有URL本质上都由web.config重写验证。 Is there a way that I can create routes in Angular that will return the correct template whether or not a file exists and if not return a custom 404 page? 有没有一种方法可以在Angular中创建路由,无论文件是否存在以及是否不返回自定义404页面,该路由都会返回正确的模板? As it is on my home page of my website I include a template file which displays a header ng-view and then a footer. 正如它在我网站的主页上一样,我包括一个模板文件,该文件显示页眉ng-view和页脚。 When I view a page such as http://somewebsite.com/random and random does not correspond to a page the home page will continue to load without displaying a 404 page. 当我查看诸如http://somewebsite.com/random之类的页面时,random与页面不对应,则主页将继续加载而不显示404页面。 The page will continue to load without ceasing. 该页面将继续加载而不停止。
========== web.config settings ============== ========== web.config设置==============
<system.webServer>
<rewrite>
<rules>
<rule name="AngularJS Routes" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_URI}" pattern="^/(api)" negate="true" />
</conditions>
<action type="Rewrite" url="/" />
</rule>
</rules>
</rewrite>
======= Angular Route settings =============== =======角路线设置===============
angular.module('main').config(['$routeProvider', '$locationProvider',
function($routeProvider, $locationProvider)
{
//we cannot use services or factories so using the anguar $http provider is
//out of question. Instead we will use a legacy HTTP AJAX request and return
//the status of the said page.
var checkUrlStatus = function(url){
console.log(url + '.asp');
var http = new XMLHttpRequest();
http.open('GET', url, false);
http.send();
console.log(http.status);
return (http.status !== 404 ? url : 'https://somewebsite.com/404.asp');
}
//removes the hash from the urls, but must be used in conjunction with
//the web.config or apache config file
if(window.history && window.history.pushState){
$locationProvider.html5Mode({
enabled: true,
requireBase: false
});
}
//conditions to route page to the correct template. See if the template
//returns the right resonse with the checkUrl function and proceed forward.
$routeProvider.when('/:param',{
templateUrl: function(params){
//return checkUrlStatus('https://somewebsite.com/pages/accounting.asp');
console.log(checkUrlStatus('https://somewebsite.com/pages/bob.asp') );
//return "/pages/"+url_param+".asp";
}
}).when('/',{
templateUrl: function(){
return 'home.asp';
}
}).otherwise({
templateUrl: function(){
return 'home.asp';
}
})
}
]); ]);
The problem is that I can make a request to a page however, the page will always return a 200 because the URL rewrite is returning / or the home page of my app. 问题是我可以向页面发出请求,但是该页面将始终返回200,因为URL重写返回了/或我的应用程序的主页。 So instead I created a tag "is-content" as the first class on my page and when there was an AJAX request for this page I looked for this class. 因此,我创建了一个标签“ is-content”作为页面上的第一类,并且当对此页面有AJAX请求时,我正在寻找该类。 If it existed I displayed the contents of the template or else I displayed the content for my custom 404 page. 如果存在,则显示模板的内容,否则显示自定义404页面的内容。 Below is an example of the function used to test this: 以下是用于测试此功能的示例:
var checkUrlStatus = function(url){
var http = new XMLHttpRequest();
http.open('GET', url, false);
http.send();
var responseText = http.responseText;
var htmlObject = document.createElement('div');
htmlObject.innerHTML = responseText;
var htmlReturn = htmlObject;
if ( htmlReturn.getElementsByClassName('is-content').length > 0){
return url;
}
else {
return 'https://testwebsite.com/pages/404.asp';
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.