对于想了解如果我的方法具有多个路由注释,该如何使用url_for?的读者,本文将是一篇不可错过的文章,我们将详细介绍当路由器获知多条路径,并且为您提供关于4_url_for的使用、angularjs–
对于想了解如果我的方法具有多个路由注释,该如何使用url_for?的读者,本文将是一篇不可错过的文章,我们将详细介绍当路由器获知多条路径,并且为您提供关于4_url_for的使用、angularjs – 如果我的url包含路由参数,则哈希链接重新路由为角、asp.net-mvc – 具有多个路由值的Url.Action帮助器的问题、asp.net-web-api2 – 使用未绑定功能时具有多个路由的OData服务的有价值信息。
本文目录一览:- 如果我的方法具有多个路由注释,该如何使用url_for?(当路由器获知多条路径)
- 4_url_for的使用
- angularjs – 如果我的url包含路由参数,则哈希链接重新路由为角
- asp.net-mvc – 具有多个路由值的Url.Action帮助器的问题
- asp.net-web-api2 – 使用未绑定功能时具有多个路由的OData服务
如果我的方法具有多个路由注释,该如何使用url_for?(当路由器获知多条路径)
因此,我有一个可通过多个路由访问的方法:
@app.route("/canonical/path/")@app.route("/alternate/path/")def foo(): return "hi!"
现在,我该如何致电url_for("foo")
并知道我将获得第一条路线?
答案1
小编典典好。它花了一些时间研究werkzeug.routing
和flask.helpers.url_for
代码,但我知道了。您只需更改endpoint
路线的即可(换句话说,您
为 路线 命名 )
@app.route("/canonical/path/", endpoint="foo-canonical")@app.route("/alternate/path/")def foo(): return "hi!"@app.route("/wheee")def bar(): return "canonical path is %s, alternative is %s" % (url_for("foo-canonical"), url_for("foo"))
将产生
规范路径为/ canonical / path /,替代方案为/ alternate / path /
这种方法有一个缺点。Flask始终将最后定义的路由绑定到隐式定义的端点(foo
在您的代码中)。猜猜如果您重新定义端点会发生什么?你url_for(''old_endpoint'')
会全力以赴werkzeug.routing.BuildError
。因此,我想整个问题的正确解决方案是在最后定义路径并
命名 替代项:
""" since url_for(''foo'') will be used for canonical path we don''t have other options rather then defining an endpoint for alternative path, so we can use it with url_for"""@app.route(''/alternative/path'', endpoint=''foo-alternative'')""" we dont wanna mess with the endpoint here - we want url_for(''foo'') to be pointing to the canonical path"""@app.route(''/canonical/path'') def foo(): pass@app.route(''/wheee'')def bar(): return "canonical path is %s, alternative is %s" % (url_for("foo"), url_for("foo-alternative"))
4_url_for的使用
url_for
与 django 的reverse(''app_name:name'') 类似,都是用来反转路由的
url_for
的基本使用:
url_for
: url_for的一个参数是一个视图函数的名字的字符串格式,后面的参数的参数以关键字的形式传递给url
。 如果传递的参数在那个视图中url中定义了,那么这个参数就会以路径参数的形式给url
。 如果传递的参数没有在url
中定义,那么这些参数将会以查询字符串的形式放到url
中。
from flask import Flask, url_for
app = Flask(__name__)
@app.route(''/'')
def index():
my_list_path = url_for(''my_list'', page=1, count=111)
print(my_list_path) # 结果为: /list/1/?count=111
return my_list_path
@app.route(''/list/<int:page>/'')
def my_list(page):
return ''第 {} 页''.format(page)
为什么需要 url_for
:
- 将来如果修改了
URL
, 但没有修改该URL对应的函数名,就不用到处支替换URL了. url_for
会自动的处理那些特殊的字符,不需要手动处理
@app.route('''')
def index():
a = url_for(''login'', next=''/'')
print(a) # /login/?next=%2F # 会自动编码,不需要手动处理了
return a
@app.route(''/login/'')
def login():
return ''login''
使用url_for
在模板中加载静态文件
加载静态文件使用的是url_for
函数。第一个参数需要为''static''
, 第二个参数需要为一个关键字参数filename=''静态文件路径''
。 如下所示:
<link rel="stylesheet" href="{{ url_for(''static'', filename=''css/index.css'') }}">
路径查找,<spancolor: red;''>要心当前项目的sttic
目录作为根目录</span>
强烈建议以后在使用url的时候,使用url_for来反转 url
angularjs – 如果我的url包含路由参数,则哈希链接重新路由为角
所以,如果im在页面国家,并点击链接#developing,那么页面将滚动到#developing而不重新路由,这是所需的.如果我在页面国家/地区,我点击#projects,页面将重新路由,然后滚动到#projects;我不想要重新路由发生.
这个问题只发生在大自然page1 / parameter#anchor的链接上,而不是简单的pageA#anchor.
主页的HTML代码段:
<ul> <li><a href="#/Country">Go to /Country</a></li> <li><a href="#/Country/US">Go to /Country/US</a></li> <li><a href="#/Country/Italy#Section-4">Go to /Country/Italy#Section-4</a></li> <li><a href="#/Country/Canada#Section-8">Go to /Country/Canada#Section-8</a></li> </ul>
国家/地区页面的HTML代码段:
<div id="Section-1"> Section 1 <div ng-if="country"> <a ng-href="#/Country/{{country}}#Section-8">Go to /Country/{{country}}#Section-8</a> </div> <div ng-if="!country"> <a ng-href="#/Country#Section-8">Go to /Country#Section-8</a> </div> </div>
所有JavaScript代码:
var app = angular.module("app",["ngRoute"]); app.config(["$routeProvider","$locationProvider",function ($routeProvider,$locationProvider) { $routeProvider .when("/",{ templateUrl: "./home-page.html",caseInsensitiveMatch: true,}) .when("/Home",}) .when("/Country",{ templateUrl: "./country-page.html",}) .when("/Country/:country",}) }]); countryController.$inject = ["$scope","$routeParams","$location","$anchorScroll"]; function countryController($scope,$routeParams,$location,$anchorScroll) { $scope.country = $routeParams.country; if (!!$location.$$hash) { $location.hash($location.$$hash); $anchorScroll(); } }
asp.net-mvc – 具有多个路由值的Url.Action帮助器的问题
var url = '@Url.Action("Action","Controler",new { a = "a",b = "b" })'; $.post(url).success(function(data) { ... });
问题是在此之后的URL的值是/ Controller / Action?a = a& amp; b = b,请注意& amp; amp; amp;路线值之间.这不行,但如果我这样做:
var url = '@Url.Action("Action",b = "b" })'.replace('amp;','');
它的工作完美!
我的行动是这样的:
public JsonResult Action(string a,string b) { ... }
我调试了我的应用程序,该动作被调用,甚至一个是“a”,但是b是空的.
这是Url.Action的理想行为吗?我不这么认为请帮助我,我做错了什么?我应该在Url.Action调用中添加另一个参数吗?提前致谢.
解决方法
你找到了一种方法来解决它,我看过别人.由于这个问题不是最好的方式,但无论是否是你,我可以向你保证,这不是你.你什么都没有明显错误.这是Javascript的自然行为.
如果我们讨论最好的方法,我更喜欢使用@ Url.Action来获取我的URL,并将params作为参数传递给我的JQuery方法(.Ajax方法是我最喜欢的).
例子从e.camper借来
$.post(url,{ a: "a",b: "b"}).success(...)
asp.net-web-api2 – 使用未绑定功能时具有多个路由的OData服务
我有以下内容:
config.MapODataServiceRoute("test1","test1",GetEdmModelTest1()); config.MapODataServiceRoute("test2","test2",GetEdmModelTest2());
每个GetEdmModel方法都有映射对象.
我可以按照以下方式访问该服务(这很好):
http://testing.com/test1/objects1()
http://testing.com/test2/objects2()
但是,如果我尝试调用以下函数(将无法工作):
[HttpGet] [ODaTaroute("test1/TestFunction1()")] public int TestFunction1() { return 1; }
它会抛出以下错误:
The path template ‘test1/TestFunction1()’ on the action ‘TestFunction1’ in controller ‘Testing’ is not a valid OData path template. Resource not found for the segment ‘test1’.
然而,如果我删除“test2”的“MapODataServiceRoute”,那么只有一条路线,它一切正常.
如何使用多个路由?
**我已在以下**发布了该问题的完整示例
https://github.com/OData/WebApi/issues/1223
**我已尝试下面列出的OData版本示例,但有以下问题**
https://github.com/OData/ODataSamples/tree/master/WebApi/v4/ODataVersioningSample
我之前尝试过“OData版本”示例,但它没有用.
似乎未绑定(未绑定是目标)不遵循相同的路由规则是正常的服务调用.
防爆.如果您下载“OData版本”示例并执行以下操作.
>在V1 – > WebApiConfig.cs添加
builder.Function(nameof(Controller.ProductsV1Controller.Test))返回<串GT;();
>在V2中 – > WebApiConfig.cs添加
builder.Function(nameof(Controller.ProductsV2Controller.Test))返回<串GT;();
>在V1 – > ProductsV1Controller.cs添加
[HTTPGET]
[ODaTaroute( “测试()”)]
public string test()
{return“V1_Test”; }
>在V2中 – > ProductsV2Controller.cs添加
[HTTPGET]
[ODaTaroute( “测试()”)]
public string test()
{return“V2_Test”; }
现在打电话给它. “/ versionbyroute / v1 / test()”你会得到“V2_Test”
问题是“GetControllerName”在使用未绑定的函数/操作时不知道如何获取控制器.
这就是我尝试“推断”控制器时发现的大多数示例代码失败的原因.
解决方法
The key point of trouble is usually that the DefaultHttpControllerSelector maps controllers by local name,not fullname/namespace.
如果您的实体类型和控制器名称在两个EdmModels中都是唯一的,那么您不需要做任何特殊的事情,它应该只是开箱即用.上面的示例利用了这个概念,强制您将字符串值注入控制器类的物理名称以使它们唯一,然后在ODataVersionControllerSelector中重写GetControllerName以将传入路由映射到自定义控制器名称
如果控制器的唯一名称看起来很难,并且您希望使用完整的命名空间(意味着您的控制器名称逻辑仍然是标准的),那么您当然可以在重写DefaultHttpControllerSelector时实现自己的逻辑来选择特定的控制器类实例.简单地重写SelectController.这个方法需要返回一个HttpControllerDescriptor的实例,它比样本更复杂一些.
为了向您展示我的意思,我会将解决方案发布到旧项目的要求中,这与您的项目略有不同.我有一个WebAPI项目来管理对多个数据库的访问,这些数据库具有类似的模式,许多实体名称是相同的,这意味着这些控制器类将具有相同的名称.控制器由文件夹/命名空间构成,这样就有一个名为DB的根文件夹,然后每个数据库都有一个文件夹,然后控制器就在那里.
You can see that this project has many different schemas,they effectively map to versions of an evolving solution,the non-DB namespaces in this image are a mix of OData v4,v3 and standard REST apis. It is possible to get all these beasts to co-exist
总结
以上是小编为你收集整理的asp.net-web-api2 – 使用未绑定功能时具有多个路由的OData服务全部内容。
如果觉得小编网站内容还不错,欢迎将小编网站推荐给好友。
关于如果我的方法具有多个路由注释,该如何使用url_for?和当路由器获知多条路径的介绍现已完结,谢谢您的耐心阅读,如果想了解更多关于4_url_for的使用、angularjs – 如果我的url包含路由参数,则哈希链接重新路由为角、asp.net-mvc – 具有多个路由值的Url.Action帮助器的问题、asp.net-web-api2 – 使用未绑定功能时具有多个路由的OData服务的相关知识,请在本站寻找。
本文标签: