0

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.

1
  • 1
    Its just a .cshtml file that you'd create in the App_Code folder (right-click on the project->Add->Add ASP.NET Folder->App_Code). You don't have to do anything to use the base url, it tells the browser where to request from. So if you set base url to /application/site when requesting /page.html it would look at /application/site/page.html. Commented Oct 31, 2014 at 21:43

3 Answers 3

3

The best way to generate your action url is :

@Url.Action("ActionName", "ControllerName", new { area = "AreaName" })
Sign up to request clarification or add additional context in comments.

Comments

2

What I've found works best is to define your base url in the html in your layout

<html lang="en">
<head>
    <meta charset="utf-8" />
    <base href="@Functions.GetApplicationPath(Request)" />
    // Other header stuff
</head>

And define that @Functions in your App_Code folder (might have to create it)

@functions {

    public static string GetApplicationPath(HttpRequestBase request)
    {
        // Gets the base url in the following format: "http(s)://domain(:port)/AppPath);
        var applicationPath = request.Url.Scheme + "://" + request.Url.Authority + GetApplicationType(request);
        if (string.IsNullOrWhiteSpace(applicationPath) || !applicationPath.EndsWith("/"))
        {
            applicationPath += "/";
        }

        return applicationPath;
    }

    public static string GetApplicationType(HttpRequestBase request)
    {
        var applicationType = request.ApplicationPath;
        return applicationType;
    }
}

This lets you separate out your cshtml and your javascript so you can use relative urls and the base url will handle any website prefixes like http://yoururl/blah/actualsiteHere

Comments

1

If you are using a script tag within a Razor view, you can use the UrlHelper:

'sAjaxSource': '@Url.Action("index", "categories", new { area = "admin" })'

Otherwise, you can also make it a root-based URL with:

"/admin/Categories"

5 Comments

oh sorry, My bad. Admin is the Area, Categories is the Controller and Index is the Action. I didnt wrote Index because it was going there automatically. I am using MVC Areas.
@SizzlingCode As M.Azad shows, it still works, you just have to add the area route value.
Yes, i copied his code and it was working under areas. Thankyou.
@SizzlingCode I added an answer that lets you have absolute urls without needing to use javascript in your razor views.
@John Thankyou, I am new in ASP.Net and will surely look and will try to understand you answer. Can you please tell what should be the File Name and type under App_Code Directory and do i have to use namespaces? plus how to call baseurl in my views? If possible please Answer under your Answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.