0

我正在使用 MVC 5,我有这个网址:

/乔斯工作室埃塞克斯

这是 2 个单独的参数 {business}-{county}

Q1)我怎样才能写一条路线来拆分它?

Q2) 任何人都可以想办法处理这个 url '/joes-studio-west-yorkshire',县是 'west-yorkshire' 吗?

谢谢

4

1 回答 1

0

MVC 无法确定如何拆分该字符串,因此您必须自己做。使用此签名创建一个操作方法:

public ActionResult Index(string businessAndCounty)
{

}

在函数内部,您可以手动拆分字符串。请注意,这是完全未经测试的,实际上是伪代码,因此需要修复并添加大量错误处理:

string[] parts = businessAndCounty.Split('-');

int index = 0;
string business = parts[index];

while(!BusinessExists(business) && index < parts.Count)
{
    index++;
    business = string.Format("{0}-{1}", business, parts[index]);
}

string county = string.Join(parts.Skip(index), "-");

return BusinessAndCounty(business, county);

所以现在你的控制器看起来像这样:

public class HomeController
{
    public ActionResult Index(string businessAndCounty)
    {
        string[] parts = businessAndCounty.Split('-');

        int index = 0;
        string business = parts[index];

        while(!BusinessExists(business) && index < parts.Count)
        {
            index++;
            business = string.Format("{0}-{1}", business, parts[index]);
        }

        string county = string.Join(parts.Skip(index), "-");

        return BusinessAndCounty(business, county);
    }

    private bool BusinessExists(string business)
    {
        //Your logic to determine if the business exists
    }

    private ActionResult BusinessAndCount(string business, string county)
    {
        //your logic here
    }
}
于 2014-07-11T08:18:33.737 回答