• 保存到桌面  加入收藏  设为首页
C#/.NET

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

时间:2017-11-24 21:53:39   作者:江节胜   来源:胜行天下网   阅读:984   评论:0


调试时尽量关闭自定义错误页(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 }
        );
    }
}

更多解释



有任何疑问或技术合作都可联系我

微信:767000122 【推荐】

QQ:596957738


相关评论

加我微信 596957738 (QQ同号)加我微信   QQ联系:596957738   地址:江苏省南京市雨花台区

苏公网安备32011402010305号

皖ICP备16019989号

江节胜的Gitee,江节胜的Git地址