1

I have a MVC4 application and have hosted on IIS8.5 with published file. I have a link on my cshtml page

<a href='/home/ContactGrabber'>Import Contacts</a>

Here home is controller and ContactGrabber is an action of controller. When I am clicking on this link it show 404 error because url is showing

http://localhost/home/ContactGrabber

It should be

http://localhost/cruuntest/home/ContactGrabber

But when I am running my development code without hosting on IIS. it works fine.

Can anybody help me on this?

1 Answers1

2

Use Url.Action helper for this, so that your url is generated right, by this way you wull face issues:

<a href='@Url.Action("ContactGrabber","home")'>Import Contacts</a>

you can see details of it on MSDN

http://msdn.microsoft.com/en-us/library/dd505232%28v=vs.118%29.aspx

You can also use Html.ActionLink to generate the anchor tag:

Razor:

Html.ActionLink("Import Contacts", 
                "ContactGrabber",   // <-- ActionMethod
                "home",  // <-- Controller Name.
                 null, // <-- Route arguments.
                 null  // <-- htmlArguments .. which are none.
                )

MSDN docs

Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
  • yes you are correct but what about in jquery if I am providing var url = "/ContactGrabber/GetData" + location.search; and using $.ajax it also does not work. –  Jun 26 '14 at 16:50
  • simply this: ``var url = '@Url.Action("GetData","ContactGrabber")'+location;`` – Ehsan Sajjad Jun 26 '14 at 16:51
  • in js file not sure as i never used in seperate js file but in the view it will work – Ehsan Sajjad Jun 26 '14 at 16:52
  • I have some urls in js also and having this issue. –  Jun 26 '14 at 16:53
  • for external js files you have to go with this approach:http://stackoverflow.com/questions/13640559/asp-net-mvc-url-action-in-external-js-file – Ehsan Sajjad Jun 26 '14 at 16:55