我已经静态映射了所有控制器,除了一个... LookupController
我希望在root下运行。
以下是我的路线:
routes.MapRoute(
name: "Root",
url: "",
defaults: new {controller = "Home", action = "Index"}
);
routes.MapRoute(
name: "Home",
url: "home/{action}/{id}");
routes.MapRoute(
name: "User",
url: "user/{action}/{id}");
routes.MapRoute(
name: "Company",
url: "company/{action}/{id}");
routes.MapRoute(
name: "Recruiter",
url: "recruiter/{action}/{id}");
routes.MapRoute(
name: "Lookup",
url: "{company}/{recruiter}",
defaults: new { controller = "Lookup", action = "Index" }
);
除Lookup
的最后一个条目外,一切正常。
这会打破一切,包括其他路线。
基本上,我希望我的网址为:
/Home
/User
/Company
/Recruiter
/{company}/{recruiter}
知道我怎么能做到这一点吗?
非常感谢您的进步!
编辑:按要求添加LookupController:
public class LookupController : Controller
{
private RecruiterSightContext db = new RecruiterSightContext();
// GET: Lookup
public ActionResult Index(string company, string recruiter)
{
if (string.IsNullOrEmpty(company) && string.IsNullOrEmpty(recruiter))
{
TempData["errorMessage"] = "No Company or Recruiter specified in URL";
return RedirectToAction("Index", "Home");
}
// Company lookup
if (string.IsNullOrEmpty(recruiter))
{
Company foundCompany = db.Companies.FirstOrDefault(c => company.ToLower().Equals(c.Slug.ToLower()));
if (foundCompany == null)
{
return RedirectToAction("Index", "Home");
}
return RedirectToAction("Display", "Company", new {companyId = foundCompany.CompanyId});
}
Recruiter foundRecruiter = db.Recruiters.FirstOrDefault(r => r.Slug.ToLower().Equals(recruiter.ToLower()));
if (foundRecruiter == null)
{
return RedirectToAction("Index", "Home");
}
return RedirectToAction("RecruiterView", "Recruiter", new {recruiterId = foundRecruiter.RecruiterId});
}
}
答案 0 :(得分:0)
如果您的Lookup
文件夹存在:
尝试改变:
发件人:强>
routes.MapRoute(
name: "Lookup",
url: "{company}/{recruiter}",
defaults: new { controller = "Lookup", action = "Index" }
);
以强>
routes.MapRoute(
name: "Lookup",
url: "company/recruiter",
defaults: new { controller = "Lookup", action = "Index" }
);
然后在Browser
像这样添加:
company/recruiter
如果你的Lookup与index不同,那么我认为你需要创建另一个调用Lookup的函数:
routes.MapRoute(
name: "Lookup",
url: "company/recruiter",
defaults: new { controller = "Lookup", action = "LookupWithParameter" }
);
然后在你的控制器中:
public ActionResult LookupWithParameter(String Company, string recruiter)
不要忘记创建另一个.cshtml
文件来重定向查找页面,该页面位于Lookup folder
以下是该方案:
假设您当前正在Index.cshtml
控制器中的Home
页面上工作,那么您想要重定向到查找页面。
索引(主页).cshtml文件:
<div id="content">
@{
if (IsPost) {
<!--this option if you don't need to change another page-->
string company = Request["Company"];
string recruiter = Request["Recruiter"];
<p>You entered: <br />
Company Name: @company <br />
Recruiter Name: @recruiter</p>
}
else
{
<form method="post" action="company/recruiter">
Company Name:<br />
<input type="text" name="Company" value="" id="idcompany" /><br />
Contact Name:<br />
<input type="text" name="Recruiter" value=""id="idrecruiter" /><br /><br />
<input type="submit" value="Submit" class="submit" />
</form>
}
}
</div>
<强> LookupController:强>
public class LookupController : Controller
{
//
// GET: /Lookup/
public ActionResult MyLookup(string Company, string Recruiter)
{
// this is how to pass the value of parameter company and recruiter input from index.
//Your Action here and you can use to Debug while runnning notice that the company and the recruiter has a content from index home input
return View();
}
}
<强> RouteConfig.cs 强>
routes.MapRoute(
name: "Lookup",
url: "company/recruiter",
defaults: new { controller = "Lookup", action = "MyLookup" }
);
确保您拥有包含Lookup
MyLookup.cshtml
文件夹