原创

C#-积累-在ASP.NET MVC 两种URL请求-带参数-Resultful-RouteConfig-路由配置-带问号

调试时尽量关闭自定义错误页(Web.config)
<system.web>
<authentication mode="None" />
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
<!--错误页开启或关闭-->
<customErrors mode="Off" defaultRedirect="/home/notfound">
<error statusCode="403" redirect="/home/notfound" />
<error statusCode="404" redirect="/home/notfound" />
</customErrors>
</system.web>

方式一:使用?带参数
// localhost:29035/Home/Detail?designId=17
public ActionResult Detail(string designId)
{
if (string.IsNullOrWhiteSpace(designId))//无参数时跳转到首页
{
return RedirectToAction("Index", "Home");
}
ViewBag.DesignId = designId;
ServerResponse<OpusEntity> qer = DesignDetialDAL.GetBaseinfo(designId);
if (qer.IsSuccess())
{
OpusEntity qe = qer.GetResult();
ViewBag.BaseInfo = qe;//含有url
return View("~/Views/Home/Datail.cshtml");
}
return RedirectToAction("notfound", "Home", new { ErrCode = 404, ErrMsg = "此详情页未找到" });//404
}

方式二:RouteConfig配置路由(注: 方式一中的控制器也是要写的)
// localhost:29035/Home/Detail/17
App_Start/RouteConfig.cs 文件

public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

routes.MapRoute(
name: "Default",//唯一
url: "{controller}/{action}",
defaults: new { controller = "Home", action = "Index" }
);
routes.MapRoute(
name: "DesignDetail",
url: "{controller}/{action}/{designId}",
defaults: new { controller = "Home", action = "Detail",id = UrlParameter.Optional }
正文到此结束
本文目录