Search code examples
asp.netasp.net-mvcasp.net-mvc-5

asp.net mvc 5 need base url for ajax call


I am new in ASP.Net.

I want to have URLs like:

http://example.com/Controller/Action

Right now I am calling URL from script is like this:

$('#datatable').dataTable({
    "aoColumnDefs": [{ 'bSortable': false, 'aTargets': [-1] }],
    "oLanguage": { "oPaginate": { "sPrevious": "", "sNext": "" } },
    "iDisplayLength": 5,
    "aLengthMenu": [[5, 10, 25, 50, -1], [5, 10, 25, 50, "All"]],
    "sDom": 'T<"panel-menu dt-panelmenu"lfr><"clearfix">tip',
    "oTableTools": {
        "sSwfPath": "vendor/plugins/datatables/extras/TableTools/media/swf/copy_csv_xls_pdf.swf"
    },
    "sAjaxSource": "admin/Categories",
});

Currently using relative URL:

"sAjaxSource": "admin/Categories/Index/"

The problem with this URL is it is relative URL, but I want to define the base URL before defining the controller and action.

How can I implement this in .cshtml file under <script> tag?

If I am doing it the wrong way, then I would be keen to learn the right way to implement the URLs for AJAX calls?

Update

Sorry, I forgot to mention, I am using MVC Areas, I want to have Base URL for Areas. Admin is defined under Areas directory.


Solution

  • The best way to generate your action url is :

    @Url.Action("ActionName", "ControllerName", new { area = "AreaName" })