[英]Classifying routes based on url parameter name in Asp.net Core
[英]Is it possible to include the application name in the asp.net core routes?
我有一个简单的电话簿应用程序,它允许用户通过部门下拉列表或通过文本输入按姓名搜索员工。 我处理搜索的方式只是根据他们键入的内容或他们在下拉菜单中选择的任何选项,用 javascript 更改 html 表单的操作属性:
function selectedOption(option) {
var selValue = option.value;
document.getElementById("search-form").action = "/home/index/" + selValue;
}
这适用于本地主机,但是当我将它托管在 IIS 上时:
machineName/appName
变成
machineName/home/index/selValue
切断应用名称并返回 404 错误
我能够解决这个问题的唯一方法是使用一些硬编码来检查路径中是否已经存在“home/index”......
function selectedOption(option) {
var selValue = option.value;
if (window.location.href.includes("home")) {
var searchDest = selValue
} else {
var searchDest = window.location.href + "/home/index/" + selValue
}
document.getElementById("search-form").action = searchDest;
}
这可行,但不是很干净或传统。 有什么方法可以在 Startup.cs 中配置应用程序名称吗? 我在这里的另一个问题中看到了同样的问题,但是当我尝试添加另一个 MapRoute 时出现语法错误。 这就是我目前所拥有的一切:
app.UseMvc(routes =>
{
routes.MapRoute("default", "{controller=Home}/{action=Index}/{id?}");
});
知道如何解决这个问题吗?
正如@Desmond Chin
上面提到的,对于cshtml
,您可以使用HTML
助手。 我认为这种情况下最好的方法是使用@Url.Action(actionName, controllerName)
而不是@Url.Content()
- 它用于js
、 css
、 img
等。
您可以使用下面的代码来实现这一点:
function selectedOption(option) {
var selValue = option.value;
document.getElementById("search-form").action = '@Url.Action("Index", "Home")?<<VARIABLENAMEHERE>>'+selValue;
}
Html 助手将照顾您的 url、虚拟路径等
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.