ASP. NET MVC URL App

Source: Internet
Author: User
Tags httpcontext

 //url encryption and decryption 
string res1 = Httputility.urlencode ( " 7z2k5lgk/ii= " ); // string res2 = Httputility.urlencode (res1); // string res3 = Httputility.urldecode (res2); // string res4 = Httputility.urldecode (RES3);

//
usingGiftweb.dao;usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text.RegularExpressions;usingsystem.web;usingSYSTEM.WEB.MVC;namespacegiftweb.controllers{ Public classLogincontroller:controller {//        //GET:/login/         PublicActionResult Index () {//1. When the session expires automatically            stringRETURNURL =Request.rawurl; stringURL =string.            Empty; if(! Regex.IsMatch (RETURNURL,@"^/login/index") ) {URL=Httputility.urlencode (RETURNURL); }            //gets the ULR address at exitURL = request["RETURNURL"]; Viewbag.returnurl=URL; returnView (); } [HttpPost] PublicActionResult Index (stringLoginNamestringpassword) {                        using(Giftdbentities db =Newgiftdbentities ()) {                varUserInfo = db. Sysuserinfo.where (U = u.loginname = = LoginName && U.password = =password).                FirstOrDefault (); if(UserInfo = =NULL) {viewbag.msg="logon name or password error"; returnView (); }                Else                {                    //gets the ULR address at exit                    stringURL = request["RETURNURL"]; //Decryption of URLs                    stringresult =httputility.urldecode (URL); session["SysUserInfo"] =UserInfo; if(string. IsNullOrEmpty (URL)) {returnRedirecttoaction ("Index","Topicinfo"); }                    Else                    {                        returnredirectpermanent (Result); }                }            }        }         PublicActionResult Logout () {//gets the current URL            stringURL = request["RETURNURL"]; //Encrypt the URL            stringresult =httputility.urlencode (URL); //let the session expireSession.Abandon (); //Jump to login page            returnContent (Result); }    }}
 <Scripttype= "Text/javascript">        functionlogout () {varURL= "@Request. RawUrl";            alert (URL); $.post ("/login/logout?returnurl="+URL,function(data) {if(Data!= NULL) {window.location= "/login/index?returnurl=" +data;        }            }); }    </Script>

The URL gets very simple, ASP. NET generic:
"1"
get the full URL
(Protocol name + domain + virtual directory name + file name + parameter)

String url=request.url.tostring ();

"2" gets the virtual directory name + page name + parameters:

String Url=request.rawurl;
(or string
Url=request.url.pathandquery;)

"3" get
Virtual directory name + page name:
String Url=httpcontext.current.request.url.absolutepath;
(or
String url= HttpContext.Current.Request.Path;)


"4" gets the domain name:
String
Url=httpcontext.current.request.url.host;


"5" gets the parameter:
String url=
HttpContext.Current.Request.Url.Query;



"6" Gets the port:
Request.Url.Port


Ii. acquisition of the current Controller and action
RouteData.Route.GetRouteData (this. HttpContext). values["Controller"]
RouteData.Route.GetRouteData (this. HttpContext). Values["Action"]
or
routedata.values["Controller"]
routedata.values["Action"]

If you can use it in the view
ViewContext.RouteData.Route.GetRouteData (this. Context). values["Controller"]
ViewContext.RouteData.Route.GetRouteData (this. Context). Values["Action"]
or
viewcontext.routedata.values["Controller"]
viewcontext.routedata.values["Action"]

ASP. NET MVC URL App

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.