OpenAuth_day1_area的使用

MVC、wepabi允许使用Area(区域),每个Area代表一个模块。每个模块均可以有Controll、Model、View等等。

但是,如果不同的Area存在着相同的Controll/Action,就会对路由表造成混乱。

  • 创建一个Area后,编译器就会自动创建一个文件AreaNameRegister.cs
namespace AreaDemo.Areas.Admin
{
    public class AdminAreaRegistration : AreaRegistration 
    {
        public override string AreaName 
        {
            get 
            {
                return "Admin";
            }
        }

        public override void RegisterArea(AreaRegistrationContext context) 
        {
            context.MapRoute(
                "Admin_default",
                "Admin/{controller}/{action}/{id}",
                new { action = "Index", id = UrlParameter.Optional }
            );
        }
    }
}

在这个文件中,AdminAreaRegistration继承自 AreaRegistration,并重写了AreaName和RegisterArea。并在RegisterArea方法中定义了一个路由(访问此区域的路由)。

注意: Application_Start()中,AreaRegistration.RegisterAllAreas()和RouteConfig.RegisterRoutes(RouteTable.Routes)的调用顺序要注意。因为路由匹配的时候是有顺序的,如果调用顺序相反,就会匹配到错误的路由。

测试:

1.  RouteConfig.RegisterRoutes(RouteTable.Routes)和AreaRegistration.RegisterAllAreas()顺序不变,在Area和外部定         义同名的Controll和Action,其他不变。

结果如下:

2.同测试1,只是在定义外部路由时RegisterRoutes,添加上 namespaces constrain

 

  • 如果在Area中定义的Controller和Action与其他的Area相同的话基于要十分注意了,很可能会让路由表迷糊。
  • 在Application_Start()中,调用AreaRegistration.RegisterAllAreas(),此方法会搜索所有的继承自AreaRegistration的类。
  public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
                namespaces: new[]{ "AreaDemo.Controllers" }
            );
        }
    }

运行正常。

3. 将AreaRegistration.RegisterAllAreas()和 RouteConfig.RegisterRoutes(RouteTable.Routes);函数顺序调换。执行结果如下:

访问外部路由正常,但是访问Area内部同名路由时报错:

 

结论:如果不加上constrain,路由匹配的时候,Area和外部的路由表可能会造成冲突。(如果有同名的控制器/方法就可能匹配到多个路由。正如测试1结果所示。)

其次,路由表匹配是有顺序的,如测试2和3所示。如果先注册Area中的路由,在匹配外部的路由。那么,当浏览器发送请求时,会先匹配到Area中的路由(如果两处的路由模板相近),但是实际上Area和外部的路由是不可能相同的,所以,即使匹配到Area中的路由,但是最后还是找不到对应的action,最终报错

 

 

python爬虫中文乱码UnicodeEncodeError: 'latin-1' codec can't encode characters in position 0-4: ordinal not in range(256) >>> import cdsapi >>> >>> dataset = "derived-era5-land-daily-statistics" >>> request = { ... "variable": ["skin_temperature"], ... "year": "2018", ... "month": "01", ... "day": [ ... "01", "02", "03", ... "04", "05", "06", ... "07", "08", "09", ... "10", "11", "12", ... "13", "14", "15", ... "16", "17", "18", ... "19", "20", "21", ... "22", "23", "24", ... "25", "26", "27", ... "28", "29", "30", ... "31" ... ], ... "da\ily_statistic": "daily_maximum", ... \ "time_zone": "utc+00:00", ... \ "frequency": "1_hourly", ... \ "area": [53, 73.33, 3, 136] ... \ } >>> >>> client = cdsapi.Client() >>> client.retrieve(dataset, request).download() 2025-05-13 18:20:46,868 INFO Sending request to https://cds.climate.copernicus.eu/api/v2/resources/derived-era5-land-daily-statistics Traceback (most recent call last): File "<python-input-10>", line 1, in <module> client.retrieve(dataset, request).download() ~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^ File "C:\Users\ZhuanZ(无密码)\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.13_qbz5n2kfra8p0\LocalCache\local-packages\Python313\site-packages\cdsapi\api.py", line 381, in retrieve result = self._api("%s/resources/%s" % (self.url, name), request, "POST") File "C:\Users\ZhuanZ(无密码)\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.13_qbz5n2kfra8p0\LocalCache\local-packages\Python313\site-packages\cdsapi\api.py", line 449, in _api result = self.robust(action)( url, json=request, verify=self.verify, timeout=self.timeout ) File "C:\Users\ZhuanZ(无密码)\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.13_qbz5n2kfra8p0\LocalCache\local-packages\Python313\site-packages\cdsapi\api.py", line 634, in wrapped resp = call(*args, **kwargs) File "C:\Users\ZhuanZ(无密码)\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.13_qbz5n2kfra8p0\LocalCache\local-packages\Python313\site-packages\requests\sessions.py", line 637, in post return self.request("POST", url, data=data, json=json, **kwargs) ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\ZhuanZ(无密码)\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.13_qbz5n2kfra8p0\LocalCache\local-packages\Python313\site-packages\requests\sessions.py", line 575, in request prep = self.prepare_request(req) File "C:\Users\ZhuanZ(无密码)\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.13_qbz5n2kfra8p0\LocalCache\local-packages\Python313\site-packages\requests\sessions.py", line 484, in prepare_request p.prepare( ~~~~~~~~~^ method=request.method.upper(), ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ...<10 lines>... hooks=merge_hooks(request.hooks, self.hooks), ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ) ^ File "C:\Users\ZhuanZ(无密码)\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.13_qbz5n2kfra8p0\LocalCache\local-packages\Python313\site-packages\requests\models.py", line 371, in prepare self.prepare_auth(auth, url) ~~~~~~~~~~~~~~~~~^^^^^^^^^^^ File "C:\Users\ZhuanZ(无密码)\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.13_qbz5n2kfra8p0\LocalCache\local-packages\Python313\site-packages\requests\models.py", line 602, in prepare_auth r = auth(self) File "C:\Users\ZhuanZ(无密码)\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.13_qbz5n2kfra8p0\LocalCache\local-packages\Python313\site-packages\requests\auth.py", line 95, in __call__ r.headers["Authorization"] = _basic_auth_str(self.username, self.password) ~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\ZhuanZ(无密码)\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.13_qbz5n2kfra8p0\LocalCache\local-packages\Python313\site-packages\requests\auth.py", line 57, in _basic_auth_str username = username.encode("latin1") UnicodeEncodeError: 'latin-1' codec can't encode characters in position 0-4: ordinal not in range(256)
05-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值