mvc core2.0 routing is not Workin - asp.net-core-mvc

Here i'm new to mvc core2.0 please help me why my Routing Is not working
My Routing Class
public static class ApplicationRoteProfiler
{
public static void Routeing(IRouteBuilder builder)
{
builder.MapRoute("route1", "", new
{
Controllers = "Department",
Action = "Add",
});
builder.MapRoute("route2", "Department/Add", new
{
Controllers = "Department",
Action = "Index"
});
}
This class file i register in startup.config file
public void Configure(IApplicationBuilder app)
{
app.UseStaticFiles();
app.UseMvc();
app.UseMvcWithDefaultRoute();
app.UseMvc(routes =>
{
ApplicationRoteProfiler.Routeing(routes);
});
}
When i hit my server as http://localhost:1588/Department/Add its should redirect to Department/Index But its hitting Department/Add

Should it be just Controller not Controllers??
builder.MapRoute("route1", "", new { controller = "department", action = "index" });
My 2 cents
You shouldn't use app.UseMvcWithDefaultRoute() and app.UseMvc() at the same time. You only need to pick 1 of them.
I don't see benefits of using a static class to configure routing for MVC. You can just put all the route configurations right there inside UseMvc lamba function. Also I don't think you need to put customized route specifically for your "route1" as it follows the standard MVC routing convention.
app.UseMvc(routes =>
{
// The order of these routes matters!
routes.MapRoute(
name: "route2",
template: "department/add",
defaults: new { area = "", controller = "department", action = "index" });
routes.MapRoute(
name: "default",
template: "{controller=home}/{action=index}/{id?}");
}
You can also return RedirectToAction("index"); inside your Department controller Add method so whenever /deparment/add route is hit, it redirects to /deparment/index, assuming you have the default MVC routing setup, either use the "default" route I put on #2, or use UseMvcWithDefaultRoute(). That way you don't need to create custom routes just for redirecting.
public class DepartmentController : Controller
{
public IActionResult Index()
{
return View();
}
public IActionResult Add()
{
return RedirectToAction("index");
}
}

Related

Asp.net core application Ajax post is hitting wrong action after publishing

I have hosted a new ASP.Net core application as a sub-site of another application in IIS. In local everything has been working fine. But, after publishing this application as a subsite, Ajax post request is hitting the wrong controller and action.
Using Ajax to call the BGetDetails action in BController. But the request is hitting on the AGetDetails action in AController.
I have tried a lot of ways to fix this issue, but can't do it.
If hosted this application as a separate domain everything has been fine. I have to face the issue only when hosting the application as a sub-site of another application.
I have attached the same codes. Please find those below,
Route config
app.UseMvc(routes =>
{
routes.MapRoute(
name: "yyyyyyy",
template: "Platform/GetDetails",
defaults: new { controller = "B", action = "BGetDetails" });
});
app.UseMvc(routes =>
{
routes.MapRoute(
name: "xxxxxx",
template: "{arug1}/{arug2}",
defaults: new { controller = "A", action = "AGetDetails" });
});
Ajax call:
data = { "arug1": arug1, "arug2": arug2, "arug3": arug3, "arug4": arug4, "arug5": arug5 };
$.ajax({
type: "POST",
url: "/sub-site/Platform/GetDetails",
data: data,
success: function (result) {
...
}});
Controller:
controller-1
public class AController: Controller
{
[HttpGet]
public ActionResult AGetDetails(string arug1, string arug2)
{
...........
}
}
Controller-2
public class BController : Controller
{
[HttpPost]
public ActionResult BGetDetails(string arug1, string arug2, string arug3, string arug4, bool arug5)
{
.........
}
}
Help me to resolve this issue. Thanks in advance,

.NET Core making an AJAX call from a Razor Page to a Controller

I have a .NET Core 3.1 project using Razor Pages. From it I created a simple test where I am able to make a successful Ajax call with the following code:
Index.cshtml.cs
public class IndexModel : PageModel
{
public void OnGet()
{
}
public JsonResult OnGetTest()
{
return new JsonResult("Ajax Test");
}
}
Index.cshtml
#page
#model IndexModel
<div class="text-center">
<p>Click here for ajax test.</p>
</div>
<script type="text/javascript">
function ajaxTest() {
$.ajax({
type: "GET",
url: "/Index?handler=Test",
contentType: "application/json; charset=utf-8",
dataType: "json",
error: function (xhr, status, error) {
console.log(error);
}
}).done(function (data) {
console.log(data);
});
}
</script>
However, I would like to move the Ajax method out of the Razor Page and into to a Controller so I could call it from from multiple Razor Pages. I have created a controller using the following code:
public class AjaxController : Controller
{
public JsonResult Test()
{
return new JsonResult("Ajax Test");
}
}
Startup.cs
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc(options =>
{
options.EnableEndpointRouting = false;
});
services.AddRazorPages();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseMvcWithDefaultRoute();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
});
}
}
But whatever value I use in the url for the Ajax call, I get a 404 error. Does the Controllers folder need to be in the Pages directory? Or do I need to configure some routing to use a Controller with Razor Pages?
url: "/Ajax/Test" // <-- What goes here?
Here is the current directory structure:
In Startup.cs, add this to ConfigureServices()
services.AddMvc(options => options.EnableEndpointRouting = false);
In Startupcs, also add this to Configure()
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
DisplayController.cs
public IActionResult Test()
{
return new JsonResult("Hi World");
}
Index.cshtml
<a onclick="ClickMe();">Click Me</a>
<script>
function ClickMe() {
$.get("/Display/Test", null, function (e) {
alert(e);
});
}
</script>
You need to specify a Route attribute, like this:
[Route("api/Ajax")]
public class AjaxController : Controller
{
// ...
}
It is also best to decorate each individual endpoint with a 'Method' attribute, like this:
[HttpGet]
public JsonResult Test()
{
return new JsonResult("Ajax Test");
}
Furthermore you also need to set the correct configuration in Startup.cs as shown below, add all the parts that you do not have:
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc(options =>
{
options.EnableEndpointRouting = false;
});
services.AddRazorPages();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
// lots of stuff...
// I have this after app.UseStaticFiles(), it may also work elsewhere
app.UseMvcWithDefaultRoute();
// lots of other stuff...
}
And then you should be able to call it using the path /api/Ajax/Test.

Inconsistent culture - decimal separator ignored in model binding between razor view and viewmodel

I have the following behaviour in my program:
User input for a decimal variable
A) jquery validation turned off:
1) If the user uses a comma as decimal separator, the value is stored correctly in the ViewModel
2) If the user uses a point as decimal separator, the value is multiplied by 100 (as if there was no decimal separator)
B) jquery validation turned on:
1) I get an error, that a number must be supplied
2) same Problem as A2)
However if I display a decimal value of the ViewModel in the view it is shown per default with a point as a decimal separator.
This inconsistency is confusing me and I would like to implement a consistent behaviour, but unfortunately I don't know what I am actually looking for.
The website will be localized in german and italian. The localization works without any problems, so far.
This is my
startup.cs
namespace XXX
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// Added - uses IOptions<T> for your settings.
services.AddOptions();
// Added - Confirms that we have a home for our DemoSettings
services.Configure<DatabaseSettings>(Configuration.GetSection("DatabaseSettings"));
services.AddLocalization(options => options.ResourcesPath = "Resources");
services.AddAuthentication(IISDefaults.AuthenticationScheme);
services.AddMvc()
.AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
.AddDataAnnotationsLocalization();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseStatusCodePagesWithReExecute("/Error/Index", "?i_statusCode={0}");
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseBrowserLink();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
IList<CultureInfo> supportedCultures = new List<CultureInfo>
{
new CultureInfo("de"),
new CultureInfo("it"),
};
var localizationOptions = new RequestLocalizationOptions
{
DefaultRequestCulture = new RequestCulture("de"),
SupportedCultures = supportedCultures,
SupportedUICultures = supportedCultures
};
var requestProvider = new RouteDataRequestCultureProvider();
localizationOptions.RequestCultureProviders.Insert(0, requestProvider);
app.UseRouter(routes =>
{
routes.MapMiddlewareRoute("{culture=de}/{*mvcRoute}", subApp =>
{
subApp.UseRequestLocalization(localizationOptions);
subApp.UseMvc(mvcRoutes =>
{
mvcRoutes.MapRoute(
name: "defaultLocalized",
template: "{culture=de}/{controller=Contract}/{action=Index}/{id?}");
mvcRoutes.MapRoute(
name: "error",
template: "Error/Index",
defaults: new { controller = "Error", action = "Index", culture = "de" });
mvcRoutes.MapRoute(
name: "default",
template: "{*catchall}",
defaults: new { controller = "Home", action = "Index", culture = "de" });
});
});
});
}
}
}

how to route custome url in MVC

I have return my Controller with attribute routing like below.
[Route("{CourseName}/{CourseCode}")]
public ActionResult getAllProductList(string CourseName,string CourseCode)
{
ViewBag.CourseName = CourseName;
ViewBag.CourseCode = CourseCode;
return View("CoursePage");
}
it works fine. but if there is any ajax method calls (for ex. ../controllername/methodname) from JS then its hitting my above controller/Action instead of "methodname" . Please suggest.
My RouteConfig Code:
routes.MapRoute(
name: "CoursePage",
url: "{CourseName}/{CourseCode}",
defaults: new { controller = "Course", action = "getAllProductList" },
constraints: new { CourseName = "\\d +", CourseCode = "\\d +" }
);

MVC 5 Windows Authentication redirect

I created an MVC 5 project and using Windows Authentication.
My problem is whenever an use's action is denied by the action attribute [Authorize(Roles = "Roelabc")], the browser will pop up an login alert with username/passwor.
Now I don't want this pop up, I just want if user is denied then redirect user to an customised page.
Thanks a lot.
Create a class derived from AuthorizeAttribute, will solve my problem.
public class WindowsAuthorize : AuthorizeAttribute
{
public override void OnAuthorization(AuthorizationContext filterContext)
{
base.OnAuthorization(filterContext);
if (filterContext.Result is HttpUnauthorizedResult)
{
filterContext.Result = new RedirectToRouteResult(
new RouteValueDictionary
{
{ "client", filterContext.RouteData.Values[ "client" ] },
{ "controller", "Home" },
{ "action", "Contact" },
{ "ReturnUrl", filterContext.HttpContext.Request.RawUrl }
});
}
}
}
And use it like this
[WindowsAuthorize(Roles = "User1")]
[HttpGet]
public ActionResult Index(string runDate = "")
{ ... }
You can create a view called unathorized. And in your logon class, just redirect to that view. You can use something like this in logon.cs
if (AuthenticateUser())
{
if (CheckRulesOfBehavior())
{
AuthorizeUser();
HttpContext.Response.Redirect("~/Work/Index");
}
else
{
HttpContext.Response.Redirect("~/Errors/Unauthorized");
}

Resources