Javascript 如何使用AngularJS在IIS上重写URL而不使用#?
我在web.config中使用了一个URL重写,它允许我添加不包含#的URL,并允许我查看URL,例如使用“name”作为参数来检索模板文件。当我将此参数与XMLHTTPRequest一起使用时,页面总是返回200响应,因为所有URL基本上都通过web.config重写进行验证。是否有一种方法可以在Angular中创建路由,无论文件是否存在,都可以返回正确的模板,如果不存在,则返回自定义404页面?因为它在我的网站主页上,我包含了一个模板文件,其中显示了页眉和页脚。当我查看诸如和random之类的页面时,主页将继续加载,而不显示404页面。页面将继续加载而不停止 =============web.config设置==============Javascript 如何使用AngularJS在IIS上重写URL而不使用#?,javascript,angularjs,iis,Javascript,Angularjs,Iis,我在web.config中使用了一个URL重写,它允许我添加不包含#的URL,并允许我查看URL,例如使用“name”作为参数来检索模板文件。当我将此参数与XMLHTTPRequest一起使用时,页面总是返回200响应,因为所有URL基本上都通过web.config重写进行验证。是否有一种方法可以在Angular中创建路由,无论文件是否存在,都可以返回正确的模板,如果不存在,则返回自定义404页面?因为它在我的网站主页上,我包含了一个模板文件,其中显示了页眉和页脚。当我查看诸如和random之类
<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.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';
}
})
}
])) 问题是,我可以向某个页面发出请求,但该页面将始终返回200,因为URL重写正在返回/或我的应用程序主页。因此,我创建了一个标记“is content”,作为我页面上的第一个类,当这个页面有AJAX请求时,我寻找这个类。如果存在,我会显示模板的内容,否则我会显示自定义404页面的内容。下面是用于测试此功能的函数示例:
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';
}
}
你不能。URL的“片段标识符”部分不会发送到服务器,因此服务器无法更改它。您必须在客户端脚本中执行此操作。我意识到片段部分没有发送到服务器,这就是为什么我希望向请求的文件发送ajax请求并查看其是否存在,但是当我这样做时,服务器会在检查文件是否存在之前将其重写为“/”,因此每个请求都会在200返回。是否可以在查看文件是否存在或绕过此功能后进行重写?我应该找一个特殊的id标签吗?