从控制器调用静态类

时间:2014-11-04 04:00:03

标签: asp.net-mvc class controller

我可以从视图(cshtml)调用静态类,但不能从控制器调用静态类,例如:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace TEST
{
    public static class Tool
    {
        public static Dictionary<string, string> CreateHeadTailTable()
        {
            Dictionary<string, string> source =
               new Dictionary<string, string>();

            for (int i = 0; i < 10; i++)
            {
                source.Add(i.ToString(), "");
            }
            return source;
        }
    }
}

现在在控制器中,我想使用该功能,但我不能:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TEST; 

namespace TEST.Controllers
{
    public class CityController : Controller
    {
        //
        // GET: /City/

        public string Index(string cityID)
        {
            if (string.IsNullOrEmpty(cityID) || cityID.Length < 3)
                return "error"; //RedirectToAction("Index", "Home");

            // **can call TEST.Tool.CreateHeadTailTable or Tool.CreateHeadTailTable here** 

            return "get " +  cityID.Substring(cityID.Length - 3, 3);
        }

    }
}

但我可以毫无问题地从视图(cshtml)中调用类

0 个答案:

没有答案