简体   繁体   中英

Server error in application '/'. HTTP 404. MVC ASP.NET

I am trying to show a view through the POST action method, but when calling this action it shows me the message "Server error in application '/'" .

The resource cannot be found.
Description: HTTP 404. The resource you are looking for (or one of its dependencies) may have been removed, renamed, or temporarily unavailable. Please review the URL below and make sure it is spelled correctly.

I have already created my respective view for this method using the routes.MapMvcAttributeRoutes() .

     [Route("Home/AddPiloto")]
     [Route("AddPiloto")]
     public ActionResult AddPiloto()
     {
         return View();
     }

Here is the POST action that I am calling from my html form, the method works and gets the data, only the view fails.

    [HttpPost]
    public ActionResult AddPiloto(PilotoClass pclass)
    {
        HttpClient httpClient = new HttpClient();
        httpClient.BaseAddress = new Uri("http://localhost:8080/AeronauticaDGAC/");
        var request = httpClient.PostAsync("webresources/conndatabase.piloto/supCreatePost", pclass,
            new JsonMediaTypeFormatter()).Result;
        if (request.IsSuccessStatusCode)
        {
            var resultString = request.Content.ReadAsStringAsync().Result;
            var succes = JsonConvert.DeserializeObject<bool>(resultString);
            ViewBag.Mg = succes;
            return RedirectToAction("AddPiloto");
        }
        ViewBag.Mg = request.StatusCode;
        return RedirectToAction("Index",ViewBag);
    }

Finally here I have a typical form that calls this method POST .

        <form action="AddPiloto" method="post">
            <div class="form-group">
                <input class="form-control" type="number" name="id" value="" placeholder="Id" />
                <input id="inp1" class="form-control" type="text" name="nombre" value="" placeholder="Nombre" />
                <input id="inp1" class="form-control" type="text" name="apellido" value="" placeholder="Apellido" />
                <input id="inp1" class="form-control" type="number" name="edad" value="" placeholder="Edad" />
                <hr />
                <input class="btn btn-primary" type="submit" name="button" value="Enviar" />
                <input onclick="limpiarFormulario1()" class="btn btn-danger" type="button" name="button" value="Limpiar" />
            </div>
        </form>

I already have the view created, compile and recompile the solution, clear the browser cache , check if the file exists and restart the IIS server , but nothing works for me, if someone knows any possible solution I would greatly appreciate the answer. Thanks in advance.

All code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Net.Http;
using Newtonsoft.Json;
using AeronauticaClient.Models;
using System.Net.Http.Formatting;

namespace AeronauticaClient.Controllers
{
    [RoutePrefix("Home")]
    [Route("{action}")]
    public class HomeController : Controller
    {
        [Route("~/")]
        [Route("")]
        [Route("Index")]
        [HttpGet]
        public ActionResult Index()
        {
            System.Net.Http.HttpClient CHttp = new HttpClient();
            CHttp.BaseAddress = new Uri("http://localhost:8080/AeronauticaDGAC/");
            var request = CHttp.GetAsync("webresources/conndatabase.piloto/supFindAllGet").Result;
            if (request.IsSuccessStatusCode)
            {
                var resultString = request.Content.ReadAsStringAsync().Result;
                var listado = JsonConvert.DeserializeObject<List<PilotoClass>>(resultString);
                ViewBag.Message = request;
                return View(listado);
            }
            else
            {
                ViewBag.Message = request;
            }
            return View();
        }
        
        [Route("Home/AddPiloto")]
        [Route("AddPiloto")]
        public ActionResult AddPiloto()
        {
            return View();
        }
        
        [HttpPost]
        public ActionResult AddPiloto(PilotoClass pclass)
        {
            HttpClient httpClient = new HttpClient();
            httpClient.BaseAddress = new Uri("http://localhost:8080/AeronauticaDGAC/");
            var request = httpClient.PostAsync("webresources/conndatabase.piloto/supCreatePost", pclass,
                new JsonMediaTypeFormatter()).Result;
            if (request.IsSuccessStatusCode)
            {
                var resultString = request.Content.ReadAsStringAsync().Result;
                var succes = JsonConvert.DeserializeObject<bool>(resultString);
                ViewBag.Mg = succes;
                return RedirectToAction("AddPiloto");
            }
            ViewBag.Mg = request.StatusCode;
            return RedirectToAction("Index",ViewBag);
        }
    }
}

Routing code.

public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.MapMvcAttributeRoutes();
            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }
    }

When you redirect to index, you're passing ViewBag as an argument.

return RedirectToAction("Index",ViewBag);

But the server cannot find an index method receiving that parameter, so it raises an exception.

  1. Remove ViewBag as an argument when you redirect.

     return RedirectToAction("Index");
  2. Before redirecting, replace ViewBag for TempData

     if (request.IsSuccessStatusCode) { var resultString = request.Content.ReadAsStringAsync().Result; var succes = JsonConvert.DeserializeObject<bool>(resultString); TempData["Mg"] = succes; return RedirectToAction("AddPiloto"); } TempData["Mg"] = request.StatusCode; return RedirectToAction("Index");
  3. To retrieve the value you stored in previous step, use TempData["Mg"] further on.

You need TempData instead because after RedirectToAction you'll be in a new request. Since ViewBag is only available for same request, the values would be lost after redirecting.

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