我正在尝试为我的网站添加新视图但是我继续在'/'应用程序中收到错误服务器错误。我的视图链接到控制器,我的路径配置文件具有正确的路径。任何帮助都会很感激。
配置文件控制器
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace Bitev2.Controllers
{
public class ProfileController : Controller
{
//
// GET: /Profile/
public ActionResult Index()
{
return View();
}
//
// GET: /Profile/Details/5
public ActionResult Details(int id)
{
return View();
}
//
// GET: /Profile/Create
public ActionResult Create()
{
return View();
}
//
// POST: /Profile/Create
[HttpPost]
public ActionResult Create(FormCollection collection)
{
try
{
// TODO: Add insert logic here
return RedirectToAction("Index");
}
catch
{
return View();
}
}
//
// GET: /Profile/Edit/5
public ActionResult Edit(int id)
{
return View();
}
//
// POST: /Profile/Edit/5
[HttpPost]
public ActionResult Edit(int id, FormCollection collection)
{
try
{
// TODO: Add update logic here
return RedirectToAction("Index");
}
catch
{
return View();
}
}
//
// GET: /Profile/Delete/5
public ActionResult Delete(int id)
{
return View();
}
//
// POST: /Profile/Delete/5
[HttpPost]
public ActionResult Delete(int id, FormCollection collection)
{
try
{
// TODO: Add delete logic here
return RedirectToAction("Index");
}
catch
{
return View();
}
}
}
}
个人资料视图
@{
ViewBag.Title = "Profile";
}
<h2>Profile</h2>
路由配置文件
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
namespace Bitev2
{
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
答案 0 :(得分:2)
目前,您的控制器结构只允许使用以下网址 -
/profile/index
/profile/details/id
/profile/create
/profile/edit/id
/profile/delete/id
运营后支持的网址 -
/profile/create
/profile/edit/id
/profile/delete/id
你正在尝试/profile/profile
并且它不存在。这就是你得到错误的原因。使用Custom Errors section创建配置文件操作或尝试处理404错误。
答案 1 :(得分:1)
使用这些默认值时,它应该有效:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new {
controller = "Profile",
action = "Index",
id = UrlParameter.Optional
});
controller =“个人资料”