2

I am running into this error while using Url.Action is ASP.net MVC 5

Server Error in '/' Application. The resource cannot be found. Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly. Requested URL: /Home/AddPhoto

Here is the relevant portion of the view. The following url.action call is the one that fails : @Url.Action("AddPhoto", "Home")

  </div>
        @using (Html.BeginForm("Index", "Home", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
        {
            <div class="col-md-8">
                <textarea class="search-query span2 text-center" rows="13" style="width: 2200px; height: 50px;" id="searchText" name="searchText" placeholder="What is on your mind?" type="text" value="What is on your mind? "></textarea>
                <br />
                <a href="#" id="addphoto" onclick="window.location.href='@Url.Action("AddPhoto", "Home")'">

                    <span rel="tooltip" title="Add photo"><i class="fa fa-camera"></i></span>
                </a>
                <br />
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; <input type="reset" value="Cancel" name="Command" style="text-align:right" />&nbsp; &nbsp;<input type="submit" name="Command" value="Post" style="text-align:right" />
                <br />
                <br />

In my HomeController.cs file, I do have this method:

public ActionResult AddPhoto()
{
   return View();
}

And the actual view file , does exist under Views/Home

Screen grab of views/home directory

I am hence not understanding the reason why I am hitting the 404 not found error. Can someone please help me out over here?

7
  • 1
    When you go to /Home/AddPhoto using the browser URL, is the page served? Commented May 5, 2016 at 6:07
  • 1
    try typing the url in browser location directly and check - localhost:portno/home/AddPhoto is it accessible? Commented May 5, 2016 at 6:09
  • @Ashish Kaluskar, Try this: go to Action , onclick View than select "Go to view", Than add on top of your action this : [HttpPost] Commented May 5, 2016 at 6:14
  • Bon and Karthik ., No it is not. I even tried creating a new view (a empty view without model) and tried to access it via the browser. still i get the same 404 not found error. The weird part is that I am able to view the Index.cshtml and About.cshtml views, both of which reside in the Views/Home directory. But any view that I create in that directory gives the 404 not found error Commented May 5, 2016 at 6:15
  • @Ashish Kaluskar, try to add [HttpPost] on top of Action Commented May 5, 2016 at 6:16

2 Answers 2

1

AddPhoto action in your case must be [HttpPost]. It should be [HttpGet]. Remove [HttpPost]. Only GET works from browser location bar.

Sign up to request clarification or add additional context in comments.

Comments

0

Here we go: Try this:

<a href="#" id="addphoto" onclick="window.location.href='../Home/AddPhoto'">

or

<a href="~/Home/AddPhoto" id="addphoto">`

Hope it helps;)

Comments

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.