python获取asp.net mvc项目的controller和action列表
直接上代码
import os
def findactions(path):
dict = {}
for name in os.listdir(path):
name = os.path.join(path,name).replace('\\','/')
if os.path.isfile(name):
if name.endswith('Controller.cs'):
controller = name.split('/')[len(name.split('/'))-1].split('Controller.cs')[0]
actions = [line for line in open(name,'r').readlines() if 'public' in line and 'public class' not in line]
dict[controller]=actions
else:
dict.update(findactions(name))
return dict
folder = 'E:/SomeMvcProject'
f = open('actions.txt','w')
for k,v in findactions(folder).items():
f.write(k+'\n')
for a in v:
f.write(a)
f.close()