Always use the Url.Action
or Url.RouteUrl
html helper methods to build the url to the action methods. It will take care of correctly building the url regardless of your current page/path.
Assuming your js code is inside a razor view, you can directly use the Url.Actio
n method and assign that to your js variable.
url: "@Url.Action("gravaCookie","entidades")",
It should work assuming you have an action method like this
[HttpPost]
public ActionResult gravaCookie(string id,string detalhe)
{
// to do : Return something
}
If your javascript is inside a seperate javascript file, you may build the url(s) in your razor view using the above helper methods and keep that in a variable which your external js file code can access. Always make sure to use javascript namespacing when doing so to avoid possible issues with global javascript variables.
@section Scripts
{
<script>
var myApp = myApp || {};
myApp.Urls = myApp.Urls || {};
myApp.Urls.baseUrl = '@Url.Content("~")';
myApp.Urls.gravaCookieUrl = '@Url.Action("gravaCookie","entidades")';
</script>
<script src="~/Scripts/PageSpecificExternalJsFile.js"></script>
}
And in your PageSpecificExternalJsFile.js
file, you can read it like
var urlToGrava= myApp.Urls.gravaCookieUrl
// Or With the base url, you may safely add the remaining url route.
var urlToGrava2= myApp.Urls.baseUrl+"entidades/gravaCookie";
// Use urlToGrava now
EDIT
If you simply care about the root/base url of the site, you may simply use /
as the first character of your url.
var urlToGrava2= "/entidades/gravaCookie";
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…