码迷,mamicode.com
首页 > Web开发 > 详细

asp.net mvc url应用

时间:2015-06-01 22:24:41      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:

//url加密与解密
string
res1 = HttpUtility.UrlEncode("7Z2K5Lgk/iI="); //值是7Z2K5Lgk%2fiI%3d string res2 = HttpUtility.UrlEncode(res1); //值是: 7Z2K5Lgk%252fiI%253d string res3 = HttpUtility.UrlDecode(res2); //值是7Z2K5Lgk%2fiI%3d string res4 = HttpUtility.UrlDecode(res3);

//值是7Z2K5Lgk/iI=
using GiftWeb.Dao;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Mvc;

namespace GiftWeb.Controllers
{
    public class LoginController : Controller
    {
        //
        // GET: /Login/

        public ActionResult Index()
        {
            //1.当session自动过期
            string returnUrl = Request.RawUrl;

            string url = string.Empty;

            if (!Regex.IsMatch(returnUrl, @"^/Login/Index"))
            { 
                url = HttpUtility.UrlEncode(returnUrl);
            }
            //获取退出时的ulr地址
            url = Request["returnUrl"];
            ViewBag.returnUrl = url;

            return View();
        }


        [HttpPost]
        public ActionResult Index(string loginname, string password)
        {
            
            using (GiftDBEntities db = new GiftDBEntities())
            {
                var userinfo = db.SysUserInfo.Where(u => u.LoginName == loginname && u.Password == password).FirstOrDefault();
                if (userinfo == null)
                {
                    ViewBag.msg = "登录名或密码错误";
                    return View();
                }
                else
                {
                    //获取退出时的ulr地址
                    string url = Request["returnUrl"];
                    //对url解密
                    string result = HttpUtility.UrlDecode(url);
                    Session["sysuserInfo"] = userinfo;

                    if (string.IsNullOrEmpty(url))
                    {
                        return RedirectToAction("Index", "TopicInfo");
                    }
                    else
                    {
                        return RedirectPermanent(result);
                    }
                }
            }
        }

        public ActionResult Logout()
        {
            //获取当前的url
            string url = Request["returnUrl"];
            //将url加密
            string result = HttpUtility.UrlEncode(url);
            //让session过期
            Session.Abandon();
            //跳转到登录页面
            return Content(result);
        }

    }
}
 <script type="text/javascript">

        function logout()
        {
            var url = "@Request.RawUrl";
            alert(url);
            $.post("/Login/Logout?returnUrl="+url, function (data) {
                if (data != null)
                {
                    window.location = "/Login/Index?returnUrl=" + data;
                }
            });
        }
    </script>

 

URL的获取很简单,ASP.NET通用:
【1】
获取 完整url

(协议名+域名+虚拟目录名+文件名+参数)

string url=Request.Url.ToString();

【2】获取 虚拟目录名+页面名+参数:

string url=Request.RawUrl;
(或 string
url=Request.Url.PathAndQuery;)

【3】获取
虚拟目录名+页面名:
string url=HttpContext.Current.Request.Url.AbsolutePath;
(或
string url= HttpContext.Current.Request.Path;)


【4】获取 域名:
string
url=HttpContext.Current.Request.Url.Host;


【5】获取 参数:
string url=
HttpContext.Current.Request.Url.Query;



【6】获取 端口:
Request.Url.Port


二、当前controller、action的获取
RouteData.Route.GetRouteData(this.HttpContext).Values["controller"]
RouteData.Route.GetRouteData(this.HttpContext).Values["action"]

RouteData.Values["controller"]
RouteData.Values["action"]

如果在视图中可以用
ViewContext.RouteData.Route.GetRouteData(this.Context).Values["controller"]
ViewContext.RouteData.Route.GetRouteData(this.Context).Values["action"]

ViewContext.RouteData.Values["controller"]
ViewContext.RouteData.Values["action"]

asp.net mvc url应用

标签:

原文地址:http://www.cnblogs.com/zoro-zero/p/4544939.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!