简体   繁体   中英

MVC 5 C# Paths change after publishing asp.net project

So this is going to bed a dumb question because I understand there is a simple solution, I just cannot figure it out....

At the moment I am running my MVC 5 project in visual studio 2013... Normally when I want to debug it I will just click run and the website will appear in the browser of my choice...

I just recently published it to my companys web server and now I am having all kinds of issues...

The website is just 15 buttons on the Home/Index page and I want to be able to click on the buttons and still run them on my localhost side and when I publish them, still have them find the right path..

Here is an example because I am not very good at explaining things... I run it in visual studio my url is:

http://localhost:54641/Home/Index

I run my published site on the webserver my url is:

http://webserver/racklabels/dakkota/Home/Index

I understand this part, and in either case both of them work but when it comes to clicking on a button, or images.. the pathing is all weird and I dont understand it.

In visual studio I click my first button and my url becomes:

http://localhost:54641/RecieveTruck/AddFor

On my webserver I click it and it turns to:

http://webserver/RecieveTruck/AddFor

but gives me the 404 error because it is not the correct route ( which should be..

http://webserver/racklabels/dakkota/RecieveTruck/AddFor

)

This is how I have my button setup in my View (Index.cshtml) and I am wondering if this is what is causing the probelm (I am thinking that I may have to do it a different way other than using onclick)

<input type="button" name="receive" value="Receive Truck" style="cursor: pointer;" class="bodyText"
    onmouseover="this.style.color='orangered';" onmouseout="this.style.color='black';"
    onclick="document.location.href='/RecieveTruck/AddForm';">

Basically I would like for this to work regardless of how many folders I am deep after I publish it and when I click run in visual studio (so I can debug/test and just republish to the webserver when I want to update)

I have tried many different ways of sending the path... like '~/RecieveTruck/AddForm' (which just adds the ~ to the address) and '../RecieveTruck/AddForm' and just 'RecieveTruck/AddForm' but none of them find the actual root of where the project lives and just adds this to it...

Can anybody help me?

You can use

@Url.Content("~/Home/Index")

but this completely misses out the benefit of mvc routing.

if you want to generate urls for controller actions, it's much better to use

@Url.Action("Index","Home")

...so now if you set up some snappy routing rule, the url in provided by .Action will adjust automagically (such as omitting url parts that have default values in your routing rules).

You need to add "~" to the front of the url document.location.href='~/RecieveTruck/AddForm'

or if that does not work use document.location.href='@Url.Content("~/RecieveTruck/AddForm")'

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