简体   繁体   中英

URLs get invalid while deploying ASP.NET MVC application from development server to production server

I know this question is old but I am trying to understand the logic behind. What am I doing?

I have created an ASP.NET MVC app and I have successfully deployed it on IIS server.

The problem?

When I deploy all the ajax calls get invalid because of appname in the url:

hostname.com/{appname}/Home/Ajaxcall 

in my ajax calls.

I am using relative urls like

$.ajax({
        url: '/CustomerAccount/Ajaxcreateinvoicesave',
        type: 'Post',
        data: data,
        success: function (res) {
            datatable.fnDraw();
        },
        error: function (response) {
        }
    });

but to runs this on server I have to write urls like

 url: '{appname}/Customeraccount/ajaxcreateinvoicesave'

What I want/want to understand?

How shared hosting or cloud hosting does not require this url rewriting. They work fine there without any modification.

Also if there any way to directly point Index without using appname in url eg

domain.com/{appname}/home/index 

to

domain.com/home/index 

in iis (in production environment)

Why are you using string literal urls, what you need it to Url.Action helper method for generating the proper relative urls so that you don't face the problem which you have stated above, you can change your urls to be generated via the method mentioned above like:

url: '@Url.Action("Ajaxcreateinvoicesave","CustomerAccount")'

If your all js is in external files then you will need to use data- attribute in html like:

<input type="button" id="btnId" 
       data-url="@Url.Action("Ajaxcreateinvoicesave","CustomerAccount")" />

and then in the client side code you would need to capture the url to send ajax call.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM