分类目录归档:后端

C#反转链表

public class ReverseLinkedList
    {
        void main()
        {
            Node list = InitList();
            Console.WriteLine(list);
            list = list.ReverseList(list);
            Console.WriteLine(list);
        }
        public Node InitList()
        {
            Node head = new Node();
            head.Next = null;
            head.Data = -1;
            Node q, p;
            p = head;
            for (int i = 1; i <= 10; i++)
            {
                q = new Node();
                q.Data = i;
                q.Next = null;
                p.Next = q;
                p = q;
            }
            return head;
        }
    }
    public class Node
    {
        private T data;
        private Node next;
        public T Data
        {
            get { return data; }
            set { this.data = value; }
        }
        public Node Next
        {
            get { return next; }
            set { next = value; }
        }
        public Node ReverseList(Node head)
        {
            if (head.Next == null || head.Next.Next == null)
            {
                return head;   /*链表为空或只有一个元素则直接返回*/
            }
            Node t = null,
                     p = head.Next,
                     q = head.Next.Next;
            while (q != null)
            {
                t = q.Next;
                q.Next = p;
                p = q;
                q = t;
            }
            /*此时q指向原始链表最后一个元素,也是逆转后的链表的表头元素*/
            head.Next.Next = null;  /*设置链表尾*/
            head.Next = p;           /*调整链表头*/
            return head;
        }
        public override string ToString()
        {
            string result = string.Empty ;
            Node p = this.Next;
            while (p != null)
            {
                result += string.Format("{0}t", p.Data);
                p = p.Next;
            }
            return result;
        }
    }

Using the ASP.NET MVC 3 Logon returnUrl Parameter

razor:

@{
  ViewBag.Title = "登陆";
  Layout = "~/Views/Shared/_LoginLayout.cshtml";
  string retUrl = "";
  if (Request["ReturnUrl"] != null)
  {
    retUrl = ViewContext.HttpContext.Request["ReturnUrl"];
  }
}
@using (Html.BeginForm("Logon", "Account", new { model = this.Model, ReturnUrl = retUrl }))
{
//form
}

controller:

[HttpPost]
public ActionResult LogOn(LogOnModel model, string ReturnUrl)
{
  ViewBag.UserLogOut = true;
  if (!ModelState.IsValid)
    return View("Logon");
  if (ModelState.IsValid)
  {
    try
    {
      //login
    }
    catch (Exception e)
    {
      ModelState.AddModelError("", e.Message);
      return View(model);
    }
  }
  if (!string.IsNullOrEmpty(ReturnUrl))
    return Redirect(ReturnUrl);//return
  return RedirectToAction("Index", "Product");
}

UserSessionAuthorizeAttribute:

public class UserSessionAuthorizeAttribute : AuthorizeAttribute
{
  public override void OnAuthorization(AuthorizationContext  filterContext)
  {
    base.OnAuthorization(filterContext);
    if(//check session)
    {
      filterContext.Result = new RedirectToRouteResult(
        new System.Web.Routing.RouteValueDictionary
            {
              { "controller", "Account" },
              { "action", "LogOn" },
              { "ReturnUrl", filterContext.HttpContext.Request.RawUrl }
            });
    }
  }
}

MVC3-Razor输出Html

输出HTML代码(包含标签):直接输出,

string html = "文本";
@html

输出HTML内容(不包含标签):有两种方法,

第一种:

IHtmlString html=new HtmlString("文本");
@html ;

第二种:

string html = "文本";
@Html.Raw(html);