在没有/ api / {controller}前缀的情况下定义Web API路由

时间:2014-10-08 13:31:23

标签: asp.net asp.net-mvc asp.net-web-api asp.net-web-api2

我想通过WebAPI控制器方法处理没有/ api / {Controller}前缀的格式的请求(例如 www.myapp.com/download/image1.jpg )。它是否可能或我是否需要切换到MVC控制器?我定义了以下路线:

config.Routes.MapHttpRoute(
                name: "Check",
                routeTemplate: "download",
                defaults: new { controller = "Download", action = "CheckAccess" });

但遗憾的是它不起作用,控制器甚至没有被创建(我在构造函数中有痕迹)

2 个答案:

答案 0 :(得分:0)

试试这个,

import threading
import Queue
class CsvWriter(threading.Thread):

    def __init__(self, resultq):
        super(CsvWriter, self).__init__()
        self.resultq = resultq
        self.writer = csv.DictWriter(open('results.csv', 'wb'))

    def run(self):
        done = False
        while not done:
            row = self.requltq.get()
            if row != -1:
                self.writer.writerow(row)
            else:
                done = True

class Crawler(threading.Thread):

    def __init__(self, inputqueue, resultq):
        super(Crawler, self).__init__()
        self.iq = inputq
        self.oq = resultq

    def run(self):
        done = False
        while not done:
           link = self.iq.get()
           if link != -1:
               result = self.extract_data(link)
               self.oq.put(result)
           else:
               done = True

     def extract_data(self, link):
          # crawl and extract what you need and return a dict
          pass


def main():
   linkq = Queue.Queue()
    for url in your_urls:
       linkq.put(url)
    resultq = Queue.Queue()
    writer = CsvWriter(resultq)
    writer.start()
    crawlers = [Crawler(linkq, resultq) for _ in xrange(10)]
    [c.start() for c in crawlers]
    [linkq.put(-1) for _ in crawlers]
    [c.join() for c in crawlers]
    resultq.put(-1)
    writer.join()

答案 1 :(得分:-1)

尝试属性路由(仅在web api 2中可用)