简体   繁体   中英

MVC 4 app one redirect gives HTTP Error 404.0 - Not Found

I have an MVC 4 application with the following folder structure:

Controllers
 >Table10Controller.cs
Views
 >Billing
   >>PercentageInstallmentTable.cshtml
 >Table10
   >>Create.cshtml
   >>Edit.cshtml
   >>Details.cshtml
   >>Delete.cshtml

PercentageInstallmentTable.cshtml contains the following actionlinks

@Html.ActionLink("Create New", "..\\Table10\\Create")
@Html.ActionLink("Edit", "..\\Table10\\Edit", new { id = item.PID }) 
@Html.ActionLink("Details", "..\\Table10\\Details", new { id = item.PID }) 
@Html.ActionLink("Delete", "..\\Table10\\Delete", new { id = item.PID })

Edit, Details, and Delete all redirect properly. For some reason, for create I get

HTTP Error 404.0 - Not Found. The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.

even though the file exists and is in the same folder as edit/details/delete.

I noticed this issue yesterday, and removing Create.cshtml from the Table10 folder and then placing it back in there fixed the issue. Today that solution isn't working, I just can't get it to work. What's happening?

I changed the actionlink to

 @Html.ActionLink("Create New", "Create", "Table10")

This brings up the path localhost:51269/Table10/Create which is correct, and when I swap out the 10 for other numbers of tables I've completed (ie; localhost:51269/Table8/Create) those work. Unfortunately with table 10 I still get the same 404 on Create.

In case this is helpful, here is Table10Controller.cs

  using System;
  using System.Collections.Generic;
  using System.Data;
  using System.Data.Entity;
  using System.Linq;
  using System.Web;
  using System.Web.Mvc;
  using BillingApp.Models;

namespace BillingApp.Controllers
{
public class Table10Controller : Controller
{
    private BillingUIEntities db = new BillingUIEntities();

    //
    // GET: /Table10/

    public ActionResult Index()
    {
        return View(db.PERCENTAGE_INSTALLMENT_TABLE.ToList());
    }

    //
    // GET: /Table10/Details/5

    public ActionResult Details(int id = 0)
    {
        PERCENTAGE_INSTALLMENT_TABLE percentage_installment_table = db.PERCENTAGE_INSTALLMENT_TABLE.Find(id);
        if (percentage_installment_table == null)
        {
            return HttpNotFound();
        }
        return View(percentage_installment_table);
    }

    public ActionResult View(int id = 0)
    {
        PERCENTAGE_INSTALLMENT_TABLE percentage_installment_table = db.PERCENTAGE_INSTALLMENT_TABLE.Find(id);
        if (percentage_installment_table == null)
        {
            return HttpNotFound();
        }
        return View(percentage_installment_table);
    }

    //
    // GET: /Table10/Create

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

    //
    // POST: /Table10/Create

    [HttpPost]
    public ActionResult Create(PERCENTAGE_INSTALLMENT_TABLE percentage_installment_table)
    {
        if (ModelState.IsValid)
        {
            db.PERCENTAGE_INSTALLMENT_TABLE.Add(percentage_installment_table);
            db.SaveChanges();
            return RedirectToAction("../Billing/PercentageInstallmentTable");
        }

        return View(percentage_installment_table);
    }

    //
    // GET: /Table10/Edit/5

    public ActionResult Edit(int id = 0)
    {
        PERCENTAGE_INSTALLMENT_TABLE percentage_installment_table = db.PERCENTAGE_INSTALLMENT_TABLE.Find(id);
        if (percentage_installment_table == null)
        {
            return HttpNotFound();
        }
        return View(percentage_installment_table);
    }

    //
    // POST: /Table10/Edit/5

    [HttpPost]
    public ActionResult Edit(PERCENTAGE_INSTALLMENT_TABLE percentage_installment_table)
    {
        if (ModelState.IsValid)
        {
            db.Entry(percentage_installment_table).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("../Billing/PercentageInstallmentTable");
        }
        return View(percentage_installment_table);
    }

    //
    // GET: /Table10/Delete/5

    public ActionResult Delete(int id = 0)
    {
        PERCENTAGE_INSTALLMENT_TABLE percentage_installment_table = db.PERCENTAGE_INSTALLMENT_TABLE.Find(id);
        if (percentage_installment_table == null)
        {
            return HttpNotFound();
        }
        return View(percentage_installment_table);
    }

    //
    // POST: /Table10/Delete/5

    [HttpPost, ActionName("Delete")]
    public ActionResult DeleteConfirmed(int id)
    {
        PERCENTAGE_INSTALLMENT_TABLE percentage_installment_table = db.PERCENTAGE_INSTALLMENT_TABLE.Find(id);
        db.PERCENTAGE_INSTALLMENT_TABLE.Remove(percentage_installment_table);
        db.SaveChanges();
        return RedirectToAction("../Billing/PercentageInstallmentTable");
    }

    protected override void Dispose(bool disposing)
    {
        db.Dispose();
        base.Dispose(disposing);
    }
}
}

Update: I made a backup of Table10 and its contents as well as the Table10Controller.cs. I then deleted them all, created a new Table 10 controller with option "MVC controller with read/write actions and views, using Entity Framework", so the Table10 folder would be created with fresh edit/delete/details/create. I then copied the contents of the controller and views over to the newly created files. Create still can't be viewed but all the others are fine. I just......don't understand this.

Solved problem, I needed to look at the controller more carefully - there was an actionresult View which I have no idea why it was in there. Removed it and everything's fine.

You are trying to do something strange. You are calling

@Html.ActionLink(string label, string actionName)

Your second parameter is not an action name since it points to a different folder (looks like an url to a view)

To correct this : Add the following actions in your Table10Controller : Create, Details, ... then in your view, you can call

@Html.ActionLink(string label, string controllerName, string actionName) 

like this :

@Html.ActionLink("Create", "Table10", "Create")

Or if you need to add some parameters (like the Id you are trying to pass to the controller)

@Html.ActionLink(string label, string controllerName, string actionName, object routeValue) 

like this :

@Html.ActionLink("Create", "Table10", "Create", new { Id = item.PID })

By doing this, you will call the action named "Create" on the controller named "Table10" with a label "Create" in you view.

Generated HTML will be :

<a href="Table10/Create">Create</a>

If you are trying to render a view inside your current view, then you should use

@Html.RenderPartial(string viewName) 

where you can use "..\\x\\y\\z\\view".

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