私は#を含まないURLを追加し、http://somewebsite.com/nameなどのテンプレートファイルを取得するためのパラメータとして "name"を使用してURLを表示できるようにするweb.configでURL書き換えを使用しました。このパラメータをXMLHTTPRequestで使用すると、すべてのURLがweb.configの書き換えによって本質的に検証されるため、ページは常に200応答を返します。ファイルが存在するかどうかにかかわらず正しいテンプレートを返し、カスタム404ページを返さない場合、Angularでルートを作成する方法はありますか?私のウェブサイトの私のホームページにあるので、ヘッダービューとフッタを表示するテンプレートファイルが含まれています。 http://somewebsite.com/randomのようなページを見て、ランダムがページに対応していない場合、ホームページは404ページを表示せずにロードし続けます。ページは停止せずにロードを続けます。AngularJSを使用してIIS上でURLなしでURLを書き直すにはどうすればよいですか?
========== 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.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';
}
})
}
])。
できません。 URLの「フラグメント識別子」部分はサーバーに送信されないため、サーバーはそれを変更できません。あなたはクライアントスクリプトでそれを行う必要があります。 – Dai
フラグメント部分がサーバーに送信されず、要求されたファイルにajax要求を送信して、それが存在するかどうかを確認する理由がわかります。ただし、そのときに、サーバーはチェックを行う前に "/"を書き換えます。ファイルが存在するかどうかを確認して、すべての要求が200で返されるようにします。ファイルが存在するかどうかを確認した後にリライトすることはできますか?私は特別なidタグを探すべきですか? –